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

无缝轮播图。

时间:2019-09-04 21:43:50      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:abs   设置   over   rip   oat   lse   btn   hang   ann   

<!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" >
    <img src="img/2.jpg" >
    <img src="img/3.jpg" >
    <img src="img/4.jpg" >
    <img src="img/5.jpg" >
    <!-- W1.复制第一张图片在最后,做过渡用 -->
    <img src="img/1.jpg" >
</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.left.onclick=function () {
//            that.changeIndex();
//        }
        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.display1();
    };
    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=-5000+"px"
      }
      else{
          this.index--
      }
      this.display2()
    };
    Banner.prototype.display1=function () {
      move(this.imgbox,{
          left:-this.imgs[0].width*(this.index)
        })
    };
    Banner.prototype.display2=function () {
        move(this.imgbox,{
            left:-this.imgs[0].width*this.index
        })
    };

    new Banner()
</script>
</html>

 

无缝轮播图。

标签:abs   设置   over   rip   oat   lse   btn   hang   ann   

原文地址:https://www.cnblogs.com/hy96/p/11461274.html

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