js原生 轮播图

 1 <div id="container">
 2         <div id="list" style="left: -600px;">
 3             <img src="img/5.jpg" alt="1" />
 4             <img src="img/1.jpg" alt="1" />
 5             <img src="img/2.jpg" alt="2" />
 6             <img src="img/3.jpg" alt="3" />
 7             <img src="img/4.jpg" alt="4" />
 8             <img src="img/5.jpg" alt="5" />
 9             <img src="img/1.jpg" alt="5" />
10         </div>
11         <div id="buttons">
12             <span index="1" class="on"></span>
13             <span index="2"></span>
14             <span index="3"></span>
15             <span index="4"></span>
16             <span index="5"></span>
17         </div>
18         <a href="javascript:;" id="prev" class="arrow">&lt;</a>
19         <a href="javascript:;" id="next" class="arrow">&gt;</a>
20     </div>
 * {
            margin: 0;
            padding: 0;
            text-decoration: none;
        }
        
        body {
            padding: 20px;
        }
        
        #container {
            position: relative;
            width: 600px;
            height: 400px;
            border: 3px solid #333;
            overflow: hidden;
        }
        
        #list {
            position: absolute;
            z-index: 1;
            width: 4200px;
            height: 400px;
        }
        
        #list img {
            float: left;
            width: 600px;
            height: 400px;
        }
        
        #buttons {
            position: absolute;
            left: 250px;
            bottom: 20px;
            z-index: 2;
            height: 10px;
            width: 100px;
        }
        
        #buttons span {
            float: left;
            margin-right: 5px;
            width: 10px;
            height: 10px;
            border: 1px solid #fff;
            border-radius: 50%;
            background: #333;
            cursor: pointer;
        }
        
        #buttons .on {
            background: orangered;
        }
        
        .arrow {
            position: absolute;
            top: 180px;
            z-index: 2;
            display: none;
            width: 40px;
            height: 40px;
            font-size: 36px;
            font-weight: bold;
            line-height: 39px;
            text-align: center;
            color: #fff;
            background-color: RGBA(0, 0, 0, .3);
            cursor: pointer;
        }
        
        .arrow:hover {
            background-color: RGBA(0, 0, 0, .7);
        }
        
        #container:hover .arrow {
            display: block;
        }
        
        #prev {
            left: 20px;
        }
        
        #next {
            right: 20px;
        }
  <script>
      window.onload = function () {
        var list = document.getElementById("list");
        var prev = document.getElementById("prev");
        var next = document.getElementById("next");
        var container = document.getElementById("container");

        function animate(offset) {
          //获取的是style.left,是相对左边获取距离,所以第一张图后style.left都为负值,
          //且style.left获取的是字符串,需要用parseInt()取整转化为数字。
          var newLeft = parseInt(list.style.left) + offset;
          list.style.left = newLeft + "px";
          if (newLeft < -3000) {
            list.style.left = -600 + "px";
          }
          if (newLeft > -600) {
            list.style.left = -3000 + "px";
          }
        }
        //点击时候设置偏移量
        prev.onclick = function () {
          animate(-600);
        };
        next.onclick = function () {
          animate(600);
        };

        // 定义定时器
        var timer;
        function play() {
          timer = setInterval(function () {
            next.onclick();
          }, 2000);
        }
        play();
        function stop() {
          clearInterval(timer);
        }

        // 鼠标移入的时候 执行停止定时函数
        container.onmouseover = stop;
        // 当鼠标移开的时候
        container.onmouseout = play;

        // 小圆点改变
        var buttons = document
          .getElementById("buttons")
          .getElementsByTagName("span");
        var index = 1;

        function buttonsShow() {
          //这里需要清除之前的样式
          for (var i = 0; i < buttons.length; i++) {
            if (buttons[i].className == "on") {
              buttons[i].className = "";
            }
          }
          //数组从0开始,故index需要-1
          buttons[index - 1].className = "on";
        }

        prev.onclick = function () {
          index -= 1;
          if (index < 1) {
            index = 5;
          }
          buttonsShow();
          animate(600);
        };
        next.onclick = function () {
          //由于上边定时器的作用,index会一直递增下去,我们只有5个小圆点,所以需要做出判断
          index += 1;
          if (index > 5) {
            index = 1;
          }
          buttonsShow();
          animate(-600);
        };
        for (var i = 0; i < buttons.length; i++) {
          // 这里使用的是立即执行函数,
          (function (i) {
            buttons[i].onclick = function () {
              var clickIndex = parseInt(this.getAttribute("index"));
              var offset = 600 * (index - clickIndex);
              animate(offset);
              index = clickIndex;
              buttonsShow();
            };
          })(i);
        }
      };
    </script>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值