zoukankan      html  css  js  c++  java
  • 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>

  • 相关阅读:
    Windows Server 2008 下解析二级域名的方法
    浅谈2D游戏设计模式--游戏剧情设计(1)
    公司做项目的几点经验总结(个人经验)
    ASP.NET 实现站内信功能(点对点发送,管理员群发)
    FusionChart 水印破解方法(代码版)
    如何使用SOIL在VS2012的 C++环境下显示图片
    C语言实现控制台中光标随意移动
    ASP.NET CMS模板培训教程
    如何用easyui+JAVA 实现动态拼凑datagrid表格(续)
    用JAVA实现插值查询的方法(算近似值,区间求法)
  • 原文地址:https://www.cnblogs.com/cmzhphp2017/p/7604734.html
Copyright © 2011-2022 走看看