带无缝滚动的轮播图(含JS运动框架)

时间:2023-03-08 16:03:18

今天学习了一下轮播图的写作,想到前一阵学过的无缝滚动得思想,所以就把轮播与滚动结合了一下。不过我的代码的神逻辑我自己都不敢恭维,在没网没参照的情况下,只能硬着头皮往下写,希望跟大家共勉吧。

js运动框架的代码如下:

 //获取样式
 function getStyle(obj,attr){
     if(obj.currentStyle){
         return obj.currentStyle[attr];
     }else{
         return getComputedStyle(obj,false)[attr];
     }
 }

 //运动框架
 //complete:time,ease,fn
 function move(obj,json,complete){
     var dis = {};
     var start = {};
     for(var name in json){
         start[name] = parseInt(getStyle(obj,name));
         dis[name] = json[name] - start[name];
     }
     complete = complete || {};
     complete.time = complete.time || 1000;
     complete.ease = complete.ease || "ease-in";

     var count = Math.floor(complete.time/30);
     var n = 0;
     clearInterval(obj.timer);
     obj.timer = setInterval(function(){
         n++;
         for(var name in json){
             switch (complete.ease){
                 case "linear":
                     var a = n/count;
                     var cur = start[name] + dis[name]*a;
                     break;
                 case "ease-in":
                     var a = n/count;
                     var cur = start[name] + dis[name]*a*a*a;
                     break;
                 case "ease-out":
                     var a = 1 - n/count;
                     var cur = start[name] + dis[name]*(1-a*a*a);
                     break;
             }

             if(name == "opacity"){
                 obj.style.opacity = cur;
                 obj.style.filter = "alpha(opacity="+cur*100 +")";
             }else{
                 obj.style[name] = cur + "px";
             }
         }

         if(n == count){
             clearInterval(obj.timer);
             if(complete.fn) complete.fn();
         }
     },30)
 }

运行的效果图如下:

  自动轮播,下方红色按钮同时变换,点击左右箭头分别到达上一张或下一张,问题主要是在第一张和最后一张上面。

带无缝滚动的轮播图(含JS运动框架)

神逻辑即将出现,请访客给出好建议哦!!

 <!DOCTYPE HTML>
 <html>
 <head>
     <title></title>
     <style>
         *{
             margin: 0;
             padding: 0;
         }
         #wrap{
             width: 590px;
             height: 340px;
             margin:100px auto;
             position: relative;
             overflow: hidden;
         }
         #box{
             position: absolute;
             left: 0;
             top: 0;
         }
         #box li{
             width: 590px;
             height: 340px;
             list-style: none;
             float: left;
         }
         #btn{
             position: absolute;
             left: 50%;
             margin-left:-100px ;
             bottom: 20px;
         }
         #btn li{
             width: 30px;
             height:30px;
             border-radius: 50%;
             background: #ccc;
             float: left;
             list-style: none;
             margin-right: 10px;
         }
         #btn li.active{
             background: red;
         }
         #wrap a{
             display: block;
             width: 50px;
             height: 50px;
             background: rgba(0,0,0,0.5);
             position: absolute;
             top: 145px;
             color: #fff;
             text-decoration: none;
             line-height: 50px;
             text-align: center;
         }
         #prev{
             left: 0;
         }
         #next{
             right: 0;
         }
     </style>
     <script src="move.js"></script>
 </head>
 <body>
     <div id="wrap">
         <ul id="box">
             <li><img src="img/1.jpg"></li>
             <li><img src="img/2.jpg"></li>
             <li><img src="img/3.jpg"></li>
             <li><img src="img/4.jpg"></li>
             <li><img src="img/5.jpg"></li>
         </ul>
         <ol id="btn">
             <li class="active"></li>
             <li></li>
             <li></li>
             <li></li>
             <li></li>
         </ol>
         <a id="prev" href="javascript:;">←</a>
         <a id="next" href="javascript:;">→</a>
     </div>
 </body>
 </html>
 <script>
     window.onload = function(){
         var oWrap = document.getElementById("wrap");
         var oBox = document.getElementById("box");
         var aBox = oBox.getElementsByTagName("li");
         var aBtn = document.getElementById("btn").getElementsByTagName("li");
         var oPrev = document.getElementById("prev");
         var oNext = document.getElementById("next");
         var iNow = 0;
         var timer = null;

         oBox.innerHTML += oBox.innerHTML;
         oBox.style.width = aBox[0].offsetWidth * aBox.length + "px";
         oBox.style.width = aBox[0].offsetWidth*aBox.length +"px";

         //红色按钮的样式
         function tab(){
             for(var i=0;i<aBtn.length;i++){
                 aBtn[i].className = "";
             }
             move(oBox,{left:-aBox[0].offsetWidth*iNow});
             aBtn[iNow].className = "active";
         }
         //下一张(神逻辑在此,擦汗!!希望访客给出更好建议)
         function next(){
             if(iNow==aBox.length/2){
                 oBox.style.left = 0;
                 iNow = 0;
             }
             for(var i=0;i<aBtn.length;i++){
                 aBtn[i].className = "";
             }
             iNow++;
             var iNew = iNow;
             if(iNew == aBtn.length) iNew = 0;
             aBtn[iNew].className = "active";
             move(oBox,{left:-aBox[0].offsetWidth*iNow});
         }

         //红色圆钮得点击效果
         for(var i=0;i<aBtn.length;i++){
             aBtn[i].index = i;
             aBtn[i].onclick = function(){
                 iNow = this.index;
                 tab();
             }
         }
         //上一张(与“上一张”一样的神逻辑!!)
         oPrev.onclick = function(){
             if(iNow<=aBox.length/2 - 1){
                 iNow+=5;
                 oBox.style.left = -aBox[0].offsetWidth*iNow + "px";
             }
             for(var i=0;i<aBtn.length;i++){
                 aBtn[i].className = "";
             }
             iNow--;
             var iNew = iNow-5;
             if(iNew == -1) iNew = aBtn.length-1;
             aBtn[iNew].className = "active";
             move(oBox,{left:-aBox[0].offsetWidth*iNow});
         }
         oNext.onclick = next;
         clearInterval(timer);
         timer = setInterval(next,2000);
         oWrap.onmouseout = function(){
             timer = setInterval(next,2000);
         }
         oWrap.onmouseover = function(){
             clearInterval(timer);
         }
     }
 </script>