首页 > 其他分享 >无缝轮播图1

无缝轮播图1

时间:2023-03-24 19:35:29浏览次数:31  
标签:function index 轮播 imgbox img 无缝 Banner left

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="../move.js"></script>
    <style>
        .banner{width:1000px;height:300px;margin: 20px auto;position: relative;overflow: hidden}
        .imgbox{position: absolute;left:0;
            height: 300px;}
        .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>
    function Banner() {
        this.left=document.querySelector("#left")
        this.right=document.querySelector("#right")
        this.imgs=document.querySelectorAll(".imgbox img");
        this.imgbox=document.querySelector(".imgbox")
        this.imgbox.style.width = this.imgs.length * this.imgs[0].offsetWidth + "px";
        this.index=0;
        this.init()
    }
    Banner.prototype.init=function () {
        var that = this;

        this.right.onclick = function () {
            that.changeIndex1();

        }
        this.left.onclick = function () {
            that.changeIndex2();
        };
    }
    Banner.prototype.changeIndex1=function () {
      if(this.index===this.imgs.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.changeIndex2=function () {
      if(this.index==0){
          //当所引到第一个时,回到倒数第二张图片,索引位length-2
          this.index=this.imgs.length-2;
          //索引设置好之后,将left的初始值设为-5000px(也就是最后一张图所显示的left值)。
          //move会从-5000走到-index*1000
          this.imgbox.style.left=-(this.imgs.length-1)*this.imgs[0].offsetWidth+"px"
//          console.log(-(this.imgs.length-1)*this.imgs[0].offsetWidth)
      }
      else{
          this.index--
      }
      this.display()
    };
    Banner.prototype.display=function () {
      move(this.imgbox,{
          left:-this.imgs[0].offsetWidth*(this.index)
        })
    };
    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>

  

长风破浪会有时,直挂云帆济沧海



标签:function,index,轮播,imgbox,img,无缝,Banner,left
From: https://blog.51cto.com/u_15961676/6147976

相关文章