• JS: 图片轮播模板——左右移动,点击编码移动,自动轮播


    <!DOCTYPE html>
    <html>

     <head>
      <meta charset="UTF-8">
      <title></title>
      <style>
       * {
        margin: 0px;
        padding: 0px;
       }
       
       #stage {
         500px;
        height: 300px;
        border: 3px solid black;
        margin: 100px;
        overflow: hidden;
        position: relative;
       }
       
       #banner {
         3000px;
        height: 300px;
        background-color: rosybrown;
       }
       
       .items {
         500px;
        height: 300px;
        color: white;
        font-size: 150px;
        text-align: center;
        line-height: 300px;
        float: left;
       }
       
       .btn-l,
       .btn-r {
         50px;
        height: 300px;
        background-color: black;
        opacity: 0.5;
        color: white;
        font-size: 40px;
        line-height: 300px;
        text-align: center;
        position: absolute;
        top: 0px;
       }
       
       .btn-l {
        left: 0px;
       }
       
       .btn-r {
        right: 0px;
       }
       
       .btn-l:hover,
       .btn-r:hover {
        cursor: pointer;
        opacity: 0.2;
       }
       
       .points {
        position: absolute;
        bottom: 10px;
        left: 200px;
        /*background-color: orangered;*/
         125px;
        height: 20px;
       }
       
       .dot {
         15px;
        height: 15px;
        border-radius: 50%;
        background-color: gainsboro;
        float: left;
        margin-right: 5px;
        cursor: pointer;
       }
       
       .active {
        background-color: red;
       }
      </style>
     </head>

     <body>
      <div id="stage">
       <div class="btn-l">
        <</div>
         <div class="btn-r">></div>
         <div id="banner">
          <div class="items" style="">1</div>
          <div class="items" style="">2</div>
          <div class="items" style="">3</div>
          <div class="items" style="">4</div>
          <div class="items" style="">5</div>
          <div class="items" style="">1</div>
         </div>
         <div class="points">
          <div data="1" class="dot active"></div>
          <div data="2" class="dot"></div>
          <div data="3" class="dot"></div>
          <div data="4" class="dot"></div>
          <div data="5" class="dot"></div>
         </div>
       </div>
     </body>

    </html>
    <script>
    // 取对象
     var btn_l = document.getElementsByClassName('btn-l')[0];
     var btn_r = document.getElementsByClassName("btn-r")[0];
     var banner = document.getElementById("banner");
     var dots = document.getElementsByClassName('dot');
    // 定义变量
     var count = 1;
     var arr = [];
    // 右侧按钮点击事件
     btn_r.onclick = function() {
      if(count < 6) {
       count++;
       arr.push(window.setInterval("move_left()", 20));
      } else if(count == 6) {
       count = 1;
       banner.style.marginLeft = 0 + 'px';
       count++;
       arr.push(window.setInterval("move_left()", 20));
      }
      for(var i = 0; i < dots.length; i++) {
       dots[i].setAttribute("class", "dot");
      }
      dots[count - 1].setAttribute("class", "dot active");
     }
    // 左侧按钮点击事件
     btn_l.onclick = function() {
      if(count > 1) {
       count--;
       arr.push(window.setInterval("move_right()", 20));
      }else if(count == 1){
       count = 6;
       banner.style.marginLeft = -2500 + 'px';   
       count--;
       arr.push(window.setInterval("move_right()", 20));
      }
      
      for(var i = 0; i < dots.length; i++) {
       dots[i].setAttribute("class", "dot");
      }
      dots[count - 1].setAttribute("class", "dot active");
     }
    // 向左移动
     function move_left() {
      if(banner.offsetLeft == (count - 1) * (-500)) {
       clear();
      } else {
       banner.style.marginLeft = banner.offsetLeft - 20 + "px";
      }

     }
    // 向右移动
     function move_right() {
      if(banner.offsetLeft == (count - 1) * (-500)) {
       clear();
      } else {
       banner.style.marginLeft = banner.offsetLeft + 20 + "px";
      }
     }
    // 清除所有间隔执行
     function clear() {
      for(var x in arr) {
       window.clearInterval(arr[x]);
      }
     }
    // 批量添加点击事件
     for(var j = 0; j < dots.length; j++) {
      dots[j].onclick = function() {
       count_s = this.getAttribute("data");
       if(count > count_s) {
        count = count_s;
        arr.push(window.setInterval("move_right()", 20));
       } else if(count < count_s) {
        count = count_s;
        arr.push(window.setInterval("move_left()", 20));
       }

       for(var i = 0; i < dots.length; i++) {
        dots[i].setAttribute("class", "dot");
       }
       this.setAttribute("class", "dot active");
      }
     }
    // 自动轮播
     function auto_move(){
      if(count < 6) {
       count++;
       arr.push(window.setInterval("move_left()", 20));
      } else if(count == 6) {
       count = 1;
       banner.style.marginLeft = 0 + 'px';
       count++;
       arr.push(window.setInterval("move_left()", 20));
      }
      for(var i = 0; i < dots.length; i++) {
       dots[i].setAttribute("class", "dot");
      }
      dots[count - 1].setAttribute("class", "dot active");
     } 
     window.setInterval("auto_move()",3000);
    </script>

  • 相关阅读:
    JS 录音
    JS学习笔记 之 作用域链
    JS学习笔记 之 预编译
    JS学习笔记 之 递归
    JS学习笔记 之 函数声明
    JS学习笔记 之 隐式类型转换
    JS学习笔记 之 typeof
    JS学习笔记 之 for循环
    JS学习笔记 之 运算符
    JS学习笔记 之 数据类型
  • 原文地址:https://www.cnblogs.com/cmzhphp2017/p/7604734.html
Copyright © 2020-2023  润新知