无缝轮播图1

复制代码
<!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>
复制代码

 简化版

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<!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>

  

posted @   菜鸟小何  阅读(287)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
点击右上角即可分享
微信分享提示