美文网首页程序员让前端飞
原生JS终极版-无缝轮播图

原生JS终极版-无缝轮播图

作者: 马大哈tt | 来源:发表于2017-11-03 21:50 被阅读0次

    现在大家一般很少自己用原生写轮播图,毕竟现在对于轮播图的插件各式各样,功能都很强大,在我看来功能最强大的轮播图插件一定是swiper,下面是swiper的官方文档,方便好用。
    官方网址:http://www.swiper.com.cn/
    今天,我要分享的是原生JS的轮播图,不能小瞧轮播图,我在写轮播图的时候就遇见很多的坑,当你使用transition时,恭喜你走进了一个大坑,当你使用计时器不当时,你也入了坑。

    无缝轮播

    首先看一下下面的html结构,想实现无缝轮播就要在你播放的第一张图片前面放最后一张图片,在最后一张后面放第一张图片,造成一种视觉的假象,实现无缝轮播。

    <div class="wrap">
            <div class="box">
                <div class="item item5">5</div>
                <div class="item item1">1</div>
                <div class="item item2">2</div>
                <div class="item item3">3</div>
                <div class="item item4">4</div>
                <div class="item item5">5</div>
                <div class="item item1">1</div>
            </div>
            <div class="pre" onclick="pre()">《</div>
            <div class="next" onclick="next()">》</div>
            <div class="dian">
                <span class="btn"></span>
                <span class="btn"></span>
                <span class="btn"></span>
                <span class="btn"></span>
                <span class="btn"></span>
            </div>
        </div>
    

    下面是css代码,无需多说

            .wrap{
                width: 500px;  height: 280px;  border: 10px blue solid;
                margin: 50px auto;  position: relative; overflow: hidden;
            }
            .box{height: 280px;   width: 3500px;  position: absolute;  left: -500px;    }
            .item{width: 500px;   height: 280px;   float: left; }
            .item{
                width: 500px;   height: 280px;  float: left;
                font-size: 100px;   color: white;   text-align: center;
                line-height: 280px;
            }
            .item1{
                background-color: red;
            }
            .item2{ background-color: yellow; } .item3{ background-color: blue; } .item4{   background-color: green; }.item5{   background-color: lightpink; }
            .pre, .next{    position: absolute; top: 110px; width: 30px;
                height: 50px;   background-color: black;    color: white;
                text-align: center;  line-height: 50px;
            }
            .next{  left: 470px;    }
            .dian{  width:100px;  height: 30px; border: 3px solid black;
                position: absolute; bottom: 10px;   left: 40%;
            }
            .btn{   width: 10px;    height: 10px;   border-radius: 50%;
                background-color: white;    float: left;    margin: 5px;
            }
    

    这是轮播图的样式


    0C59CC9B-D2BF-4EC8-9359-9C61E1DF33C6.png

    重点来了,JS控制移动部分的函数,核心是移动的函数,根据下标index计算出当前位置移动到对应图片需要多少距离l,使用计时器分10次使box移动到你所需要的图片,这样是为了保证无论从当前位置�移动到哪张张图片所需的时间都是10*20ms,这样能提高用户体验。
    而保证无缝轮播的两个临界点是第一张图和最后一张图,在next()和pre()函数中,当在轮播最后一张图(也就是视觉效果的第一张图)的位置时,立即切换到第二张图(真正的第一张图),在轮播第一张图也是如此。

    // 下标要从1开始,以为播放的第一张图前面有一张最后那张图,下标要注意对应
       var box = document.getElementsByClassName("box")[0];
        var btn = document.getElementsByClassName("btn");
        var index = 1;
        var moveTimer;
    // 点击下一张
        function next(){
            index++;
            if(index == 7){
                index = 2;
                box.style.left = "-500px";
            }
            moveWidthIndex();
            btnCol();
        }
    // 点击上一张
        function pre(){
            index--;
            if(index == -1){
                index = 4;
                box.style.left = "-2500px";
            }
            moveWidthIndex();
        }
    // 具体移动的函数
        function moveWidthIndex(){
            var l = index * -500 - box.offsetLeft;
            var count = 0;
            clearInterval(moveTimer);
            moveTimer = setInterval(function(){
                count++;
                box.style.left = box.offsetLeft + l/10 + "px";
                if(count >= 10){
                    clearInterval(moveTimer);
                    box.style.left = index * -500 + "px";
                }       
            },20);
        }
        var timer = setInterval(function(){
            // next();
        },3000);
        // document.onclick = function(){
        //  index = 5;
        //  moveWidthIndex();
        // }
    
        for(var i = 0; i < btn.length; i++){
            btn[i].index = i; 
            btn[i].onclick = function(){
                index = this.index + 1;
                console.log(index);
                moveWidthIndex();
                btnCol();
                clearInterval(timer);
                timer = setInterval(function(){
                    next();
                },3000);
                // next();
            }
        }
        // 点击圆点按钮的函数
        function btnCol(){
            for(var i = 0 ; i < btn.length ; i++){
                    btn[i].style.backgroundColor = "white";
                }
            if(index == 6){
                index = 1;
            }
            btn[index - 1].style.backgroundColor = "grey";
        }
    

    注意:最好不要使用transition,由于transition有渲染的影响,它会影响无缝功能的实现,于是我就干脆不使用transition。
    最后,吐槽一下,简书都不能发视频,还想把轮播的效果上传上来那。。。。

    相关文章

      网友评论

        本文标题:原生JS终极版-无缝轮播图

        本文链接:https://www.haomeiwen.com/subject/hyrxmxtx.html