原生JS實現輪播圖的效果

我們家的小常客發表於2018-06-13

原生JS實現輪播圖的效果:

  只要縷清了全域性變數index的作用,這個輪播圖也就比較容易實現了;另外,為了實現輪這個效果,有幾處clearInterval()必須寫上。廢話不多說,直接上程式碼,修復了幾個比較詭異的bug:

<!DOCTYPE html>
   <html>
       <head>
           <meta charset="UTF-8">
           <title></title>
           <style type="text/css">
               body,ul,p{margin:0;padding:0;}
               #boxs{position: relative;width:100%;height:460px;}
               #box{width:100%;height:460px;}
              ul{position:relative;width:100%;height:460px;}
              li{width:100%;height:460px;position:absolute;}
              img{width:100%;height:100%}
              #circle{position: absolute;bottom:0;left:50%;transform: translate(-50%,0);}
              #circle span{display:inline-block;height:20px;width:20px;background:white;vertical-align: top;border:1px solid yellow;border-radius:10px;/*float:left;*/}
              .on{background:red !important;}
              #left{position: absolute;background:red;height:80px;width:50px;line-height:80px;text-align: center;color:white;left:0;top:40%;}
              #right{position: absolute;background:red;height:80px;width:50px;line-height:80px;text-align: center;color:white;right:0;top:40%;}
          </style>
          <script type="text/javascript">
              window.onload=function(){
                  var circle_span=document.getElementById("circle").children;
                  var list=document.getElementById("ul1").children;
                  var box1=document.getElementById("boxs");
                  var btn1=document.getElementById("left");
                  var btn2=document.getElementById("right");
                  var timer=null;
                  var index=0;
                  play();
                  box1.onmouseover=function(){
                      clearInterval(timer);
                  }
                  box1.onmouseout=function(){
                      clearInterval(timer);
                      play();
                  }
                  btn1.onclick=function(){
                      index--;
                      if(index<0){
                          index=5;
                      }            
                      change(index);
                  }
                  btn2.onclick=function(){
                      index++;
                      if(index>5){
                          index=0;
                      }
                      change(index);
                  }
                  for(var i=0;i<circle_span.length;i++){
                      circle_span[i].index=i;
                      circle_span[i].onmouseover=function(){
                          clearInterval(timer);
                          change(this.index);
                          index=this.index;
                      }
                  }
                  function play(){
                      timer=setInterval(function(){
                          index++;
                          if(index>5){
                              index=0;
                          }
                      change(index);
                      },1000);
                  }
                  function change(index){
                      for(var i=0;i<circle_span.length;i++){
                          circle_span[i].setAttribute("class","");
                          list[i].style.display="none";
                      }
                      circle_span[index].className="on";
                      list[index].style.display="block";
                  }
              }            
          </script>
      </head>
      <body>
          <div id="boxs">
              <div id="box">
                  <ul id="ul1">
                     <li><img src="img/11.jpg"/></li>
                    <li><img src="img/22.jpg"/></li>
                    <li><img src="img/33.jpg"/></li>
                    <li><img src="img/44.jpg"/></li>
                    <li><img src="img/55.jpg"/></li>
                    <li><img src="img/66.jpg"/></li>
                  </ul>
              </div>
              <div id="circle">
                  <span class="on"></span>
                  <span></span>
                  <span></span>
                  <span></span>
                  <span></span>
                  <span></span>
              </div>
              <div id="left">向左</div>
              <div id="right">向右</div>
         </div>
     </body>
 </html>

 

相關文章