JavaScript编写无缝轮播图

大城市里の小女人 提交于 2019-11-29 17:22:25
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .banner{width:1000px;height:300px;margin: 20px auto;position: relative;overflow: hidden;}
        
        .imgbox img{width: 1000px;height:300px;position: absolute;left:1000px;top:0;}
        .imgbox img:nth-child(1){left:0;}

        .btns input{position: absolute;top:130px;width:40px;height:40px;border: none;background: rgba(200,200,200,0.5);}
        #left{left:0}
        #right{right:0}
    </style>
</head>
<body>
    <div class="banner">
        <div class="imgbox">
            <img src="img/1.jpg" alt="">
            <img src="img/2.jpg" alt="">
            <img src="img/3.jpg" alt="">
            <img src="img/4.jpg" alt="">
            <img src="img/5.jpg" alt="">
        </div>
        <div class="btns">
            <input type="button" id="left" value="<<<">
            <input type="button" id="right" value=">>>">
        </div>
    </div>
</body>
<script src="../move.js"></script>
<script>
    function Banner(){
        this.left = document.getElementById("left")
        this.right = document.getElementById("right")
        this.imgbox = document.querySelector(".imgbox")
        this.img = this.imgbox.children;

        this.index = 0;
        // W1.设置要走的索引
        this.iPrev = this.img.length - 1;

        this.init()
    }
    Banner.prototype.init = function(){
        var that = this;
        this.left.onclick = function(){
            that.changeIndex(-1)
        }
        this.right.onclick = function(){
            that.changeIndex(1)
        }
    }
    Banner.prototype.changeIndex = function(type){
        if(type == -1){
            if(this.index == 0){
                this.index = this.img.length-1;
                this.iPrev = 0;
            }else{
                this.index--;
                this.iPrev = this.index+1;
            }
        }else{
            if(this.index == this.img.length-1){
                this.index = 0;
                // W2.当要进来的是第0张时,走的是最后一张
                this.iPrev = this.img.length-1
            }else{
                this.index++;
                // W3.正常情况下,走的都是进来的上一张,上一张为当前-1
                this.iPrev = this.index-1;
            }
        }
        this.display(type);
    }
    Banner.prototype.display = function(type){
        // W4.根据要走的和要进来的索引
        // 先设置初始位置,然后再开始走(move)
        // 要走的:this.img[this.iPrev]
        this.img[this.iPrev].style.left = 0;
        move(this.img[this.iPrev],{left:-this.img[0].offsetWidth * type})
        // 要进来的:this.img[this.index]
        this.img[this.index].style.left = this.img[0].offsetWidth * type + "px";
        move(this.img[this.index],{left:0})
    }

    new Banner();
</script>
</html>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .banner{width:1000px;height:300px;margin: 20px auto;position: relative;overflow: hidden;}
        .imgbox{position: absolute;left:0;}
        .imgbox img{width: 1000px;height:300px;float:left;}

        .btns input{position: absolute;top:130px;width:40px;height:40px;border: none;background: rgba(200,200,200,0.5);}
        #left{left:0}
        #right{right:0}
    </style>
</head>
<body>
    <div class="banner">
        <div class="imgbox">
            <img src="img/1.jpg" alt="">
            <img src="img/2.jpg" alt="">
            <img src="img/3.jpg" alt="">
            <img src="img/4.jpg" alt="">
            <img src="img/5.jpg" alt="">
            <!-- W1.复制第一张图片在最后,做过渡用 -->
            <img src="img/1.jpg" alt="">
        </div>
        <div class="btns">
            <input type="button" id="left" value="<<<">
            <input type="button" id="right" value=">>>">
        </div>
    </div>
</body>
<script src="../move.js"></script>
<script>
    // OOA:
    // OOD:
    // OOP:
    function Banner(){
        this.left = document.getElementById("left")
        this.right = document.getElementById("right")
        this.imgbox = document.querySelector(".imgbox")
        this.img = this.imgbox.children;

        this.imgbox.style.width = this.img.length * this.img[0].offsetWidth + "px";

        this.index = 0;

        this.init()
    }
    Banner.prototype.init = function(){
        var that = this;
        this.left.onclick = function(){
            that.changeIndex(1)
        }
        this.right.onclick = function(){
            that.changeIndex(-1)
        }
    }
    Banner.prototype.changeIndex = function(type){
        if(type == 1){
            if(this.index == 0){
                this.index = this.img.length-2;
                this.imgbox.style.left = -(this.img.length-1) * this.img[0].offsetWidth + "px";
            }else{
                this.index--;
            }
        }else{
            if(this.index == this.img.length-1){
                // W2.当索引到最后一个时,回到真正的第二张图片,索引为1
                this.index = 1;
                // W3.索引设置好之后,将imgbox的left设置成初始值left:0
                // move会从left:0走到-index1*width1000
                this.imgbox.style.left = 0;
            }else{
                this.index++
            }
        }
        this.display();
    }
    Banner.prototype.display = function(){
        move(this.imgbox,{
            left:-this.index * this.img[0].offsetWidth
        })
    }

    new Banner();

</script>
</html>

上述两种写法都能实现无缝轮播图,但是出现的效果是截然不同的。

大家可以自己尝试,使用自己喜欢的效果。

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!