码迷,mamicode.com
首页 > 其他好文 > 详细

天猫轮播图

时间:2019-12-25 20:32:04      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:思想   poi   nload   target   箭头   20px   也会   slide   onclick   

天猫轮播图

??在做网页的时候,轮播图很常见,今天我们就做一种轮播图,通过轮播图来熟悉JavaScript的操作。
样式如下所示:
技术图片
执行代码如下所示:

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="utf-8">
        <style type="text/css">
            *{
                margin: 0;
                padding: 0;
            }
            .box{
                width: 800px;
                height: 300px;
                position: relative;
                overflow: hidden;
                margin: 100px auto;
            }
            .box .slider{
                width: 100%;
                height: 100%;
            }
            .box .slider ul{
                width: 1000%;
                list-style: none;
                position: absolute;
                top: 0;
                left: 0;
            }
            .box .slider img{
                vertical-align: top;
            }
            .box .slider ul li{
                float: left;
            }
            .box .scroll_nav{
                list-style: none;
                position: absolute;
                right: 20px;
                bottom: 10px;
            }
            .box .scroll_nav li{
                float: left;
                width: 20px;
                height: 20px;
                background-color: #000;
                color: #fff;
                text-align: center;
                font-size: 12px;
                font-family: "微软雅黑";
                line-height: 20px;
                cursor: pointer;
                border-radius: 50%;
                margin-right: 5px;
            }
            .box .scroll_nav li.current{
                background-color: #356acb;
            }
            .box .arr{
                width: 40px;
                height: 50px;
                font-size: 35px;
                font-family: "黑体";
                line-height: 50px;
                background-color: rgba(0,0,0,0.3); /*这种方式能让背景透明而文本不收影响*/
                color: #fff;
                text-align: center;
                position: absolute;
                top: 125px;
                font-weight: bold;
                display: none; 
                cursor: pointer;
                /* opacity: 0.1; opacity属性会让盒子半透明,与此同时它也会让黑子内部的文本半透明*/
            }
            .box .arr_prev{
                left: 5px;
            }
            .box .arr_next{
                right: 5px;
            }
        </style>
        
    </head>
    <body>
        <div class="box" id="scroll">
            <div class="slider" id="sl">
                <ul id="sl_ul">
                    <li><img src="imgs/1.jpg" alt=""></li>
                    <li><img src="imgs/2.jpg" alt=""></li>
                    <li><img src="imgs/3.jpg" alt=""></li>
                    <li><img src="imgs/4.jpg" alt=""></li>
                    <li><img src="imgs/5.jpg" alt=""></li>
                </ul>
            </div>
            <ul class="scroll_nav" id="sc_nav">
                
            </ul>
            <span class="arr arr_prev" id="prev">></span>

            <span class="arr arr_next" id="next">></span>
        </div>
    </body>
</html>

javascript代码部分如下所示:

function $(id){
                return document.getElementById(id);
            }
            //1、实现左右箭头的显示与隐藏
            sName = '';window.onload = function(){
                $('scroll').onmouseover = function(){
                    var arrs = $('scroll').getElementsByClassName('arr');
                    for (var i = 0; i < arrs.length; i++) {
                        arrs[i].style.display = 'block';
                    }
                }
                $('scroll').onmouseout = function(){
                    var arrs = $('scroll').getElementsByClassName('arr');
                    for (var i = 0; i < arrs.length; i++) {
                        arrs[i].style.display = 'none';
                    }
                }
                //2、实现轮播导航条
                //2.2动态生成导航条内容
                var imgCount = $('sl_ul').getElementsByTagName('li').length;
                //2.2
                for (var i = 0; i < imgCount; i++) {
                    var li = document.createElement('li');
                    li.innerHTML = i+1;
                    if(i == 0){
                        li.className = "current";
                    }
                    li.setAttribute('index',i);
                
                    li.onclick = function(){
                        var lis = $('sc_nav').getElementsByTagName('li');
                        for (var i = 0; i < lis.length; i++) {
                            lis[i].clas
                        }
                
                        this.className = 'current';
                        var index = parseInt(this.getAttribute('index'));
                        target = index * -800;
                    }
                    $('sc_nav').appendChild(li);
                }
                // 
                var leader = 0;
                var target = 0;
                setInterval(function(){
                    leader = leader + (target-leader)/10;
                    $('sl_ul').style.left = leader + 'px';
                }, 10)
                // 实现左右箭头点击
                $('next').onclick = function(){
                    if(target > -3200){
                        target -= 800;
                    }else{
                        target = 0;
                    }
                    setCurrent();
                }
                $('prev').onclick = function(){
                    if(target<0){
                        target += 800;
                    }
                    setCurrent();

                }
                // 导航度对选中的LI,与左右箭头切换图片后,相照应
                function setCurrent(){
                    var index = Math.abs(target/800);
                    var lis = $('sc_nav').getElementsByTagName('li');
                    for (var i = 0; i < lis.length; i++) {
                        lis[i].className = '';
                    }
                    lis[index].className = 'current';
                }
            }
            // 2.3.1点击导航项,做动画效果
                // 2.3排他思想完成点击导航项的样式

天猫轮播图

标签:思想   poi   nload   target   箭头   20px   也会   slide   onclick   

原文地址:https://www.cnblogs.com/hxz0618/p/12098474.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!