DIV CSS 佈局教程網

JS 焦點圖無限輪播完整實現思路
編輯:關於JavaScript     

本文拆解一個帶小圓點導航、左右切換與自動播放的焦點圖輪播。

一、涉及的知識點

  1. DOM 操作
  2. 定時器
  3. 事件運用
  4. JS 動畫
  5. 函數遞歸
  6. 無限滾動

二、結構與樣式

 <div id="banner" class="banner">
<ul id="list-banner" class="list-banner fn-clear" style="left:-624px;">
  <li><a href="#"><img src="images/banner4.jpg" width="624" height="200" alt="" title="" /></a></li>
  <li><a href="#"><img src="images/banner1.jpg" width="624" height="200" alt="" title="" /></a></li>
  <li><a href="#"><img src="images/banner2.jpg" width="624" height="200" alt="" title="" /></a></li>
  <li><a href="#"><img src="images/banner3.jpg" width="624" height="200" alt="" title="" /></a></li>
  <li><a href="#"><img src="images/banner4.jpg" width="624" height="200" alt="" title="" /></a></li>
  <li><a href="#"><img src="images/banner1.jpg" width="624" height="200" alt="" title="" /></a></li>
</ul>
<div class="list-num-wp">
  <div id="list-num" class="list-num fn-clear">
    <a href="#" class="hover"></a>
    <a href="#"></a>
    <a href="#"></a>
    <a href="#"></a>
  </div>
</div>
<div class="left"><a id="left" href="#"></a></div>
<div class="right"><a id="right" href="#"></a></div>
</div>
 .banner{position:relative;width:624px;height:200px;overflow:hidden;}
 .banner .list-banner{position:absolute;width:5000px;}
 .banner .list-banner li{float:left;width:624px;height:200px;}
 .banner .list-num-wp{position:absolute;bottom:7px;width:624px;height:11px;}
 .banner .list-num{width:100px;margin:0 auto;}
 .banner .list-num a{display:inline;float:left;width:11px;height:11px;margin:0 7px; background:url(../images/list-num.png) no-repeat;}
 .banner .list-num a:hover{background:url(../images/list-num-hover.png));}
 .banner .list-num a.hover{background:url(../images/list-num-hover.png);}
 .banner .left a{display:block;position:absolute;width:49px;height:49px;top:75px;left:4px;background:url(../images/arrow.gif) 0 0;filter: Alpha(opacity=50);-moz-opacity:.5;opacity:0.5;}
 .banner .right a{display:block;position:absolute;width:49px;height:49px;top:75px;right:4px;background:url(../images/arrow.gif) 0 -49px;filter: Alpha(opacity=50);-moz-opacity:.5;opacity:0.5;}

三、逐步演進

1. 左右按鈕先跑起來

 window.onload=function(){
  var prev=document.getElementById("left");
  var next=document.getElementById("right");
  var list_banner=document.getElementById("list-banner");
  next.onclick=function(){
    list_banner.style.left=parseInt(list_banner.style.left)-624+'px';
    // 注意:html 上的 ul 要加行間樣式 left:0;,否則這裡動不起來
  }
  prev.onclick=function(){
    list_banner.style.left=parseInt(list_banner.style.left)+624+'px';
  }
 }

2. 兩句很像的代碼,抽成函數

 function animate(offset){
   list_banner.style.left=parseInt(list_banner.style.left)+offset+'px';
 }
 next.onclick=function(){ animate(-624); }
 prev.onclick=function(){ animate(624); }

3. 無限滾動(假圖方案)

思路是:圖片序列首尾各放一張複製品。當位移超過最後一張的位置時瞬間拉回第一張的位置,反之則拉回最後一張。因為是瞬間跳轉,視覺上無縫銜接。

 function animate(offset){
   var newLeft=parseInt(list_banner.style.left)+offset;
   list_banner.style.left=newLeft+'px';
   if(newLeft<-2496){
     list_banner.style.left=-624+"px";
   }
   if(newLeft>-624){
     list_banner.style.left=-2496+"px";
   }
 }

4. 小圓點跟著切換

 var index=1;
 function showDot(){
   for(var i=0;i<list_num.length;i++){
     list_num[i].className="";
   }
   list_num[index-1].className="hover";
 }
 next.onclick=function(){
   animate(-624);
   index++;
   if(index>4){ index=1; }
   showDot();
 }
 prev.onclick=function(){
   animate(624);
   index--;
   if(index<1){ index=4; }
   showDot();
 }

5. 點擊小圓點跳轉

 for(var i=0;i<list_num.length;i++){
   list_num[i].onclick=function(){
     if(this.className=="hover"){
       return;               // 點自己不做任何事
     }
     var myIndex=parseInt(this.getAttribute("index"));
     var offset=-624*(myIndex-index);
     index=myIndex;
     animate(offset);
     showDot();
   }
 }

對應的 HTML 需要自訂 index 屬性:

 <div class="list-num-wp">
<div id="list-num" class="list-num fn-clear">
  <a index="1" href="#" class="hover"></a>
  <a index="2" href="#"></a>
  <a index="3" href="#"></a>
  <a index="4" href="#"></a>
</div>
</div>

關鍵細節:不能寫 var myIndex = this.index;,因為 index 是自訂屬性而非 DOM 內建屬性,點語法取不到。而 getAttribute() 兩者都能取,所以是更安全的選擇。

6. 加上緩動動畫

 function animate(offset){
   animated=true;
   var newLeft=parseInt(list_banner.style.left)+offset;
   var time=300;      // 位移總時間
   var interval=30;   // 位移間隔時間
   var speed=offset/(time/interval);            // 每次移動距離
   speed=speed>0?Math.ceil(speed):Math.floor(speed); // 可能存在小數,取整

   function go(){
     if((speed < 0 && parseInt(list_banner.style.left)>newLeft)
        ||(speed>0 && parseInt(list_banner.style.left)<newLeft)){
       list_banner.style.left=parseInt(list_banner.style.left)+speed+'px';
       setTimeout(go,interval); // 每隔 30 毫秒前進一下
     } else{
       animated=false;
       list_banner.style.left=newLeft+'px';
       if(newLeft<-2496){ list_banner.style.left=-624+"px"; }
       if(newLeft>-624){ list_banner.style.left=-2496+"px"; }
     }
   }
   go();
 }

事件入口都要加上「動畫進行中則忽略」的判斷,否則連續點擊會造成多個動畫互相搶位置,畫面亂跳:

 next.onclick=function(){
   if(!animated){ index++; }
   if(index>4){ index=1; }
   showDot();
   if(!animated){ animate(-624); }
 }
 prev.onclick=function(){
   if(!animated){ index--; }
   if(index<1){ index=4; }
   showDot();
   if(!animated){ animate(624); }
 }

「一個函數在條件成立時呼叫自身」就是遞歸,這裡正是靠 go() 的遞歸完成逐幀位移。setTimeout 本身只執行一次,之所以看起來持續運動,就是因為遞歸;若改用 setInterval 則是固定間隔觸發,兩者語義不同,別混用。

7. 自動播放

 function autoplay(){
   timer=setInterval(function(){ next.onclick(); },1000)
 }
 function stopautoplay(){
   clearInterval(timer);
 }
 banner.onmouseover=stopautoplay;
 banner.onmouseout=autoplay;
 autoplay();

8. 假圖交給 JS 生成

實務上圖片清單是動態的,把假圖寫死在 HTML 裡並不合理,應該用 JS 依首尾節點自動複製:

 var img_first=list_banner.getElementsByTagName("li")[0];
 var img_last=list_banner.getElementsByTagName("li")[3];
 list_banner.appendChild(img_first.cloneNode(true));
 list_banner.insertBefore(img_last.cloneNode(true),list_banner.getElementsByTagName("li")[0]);

三個 API 的行為:appendChild 把新節點掛到最後一個子節點之後;insertBefore 插到既有子節點之前;cloneNode(true) 為深克隆(含子內容),false 則只複製標籤本身。

延伸閱讀:本站《JavaScript 切換特效與技巧總結》《JavaScript 動畫特效與技巧匯總》《JavaScript 錯誤與調試技巧總結》等專題。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved