DIV CSS 佈局教程網

仿土豆網縮圖焦點輪播:moveElement 位移動畫完整解析
編輯:關於JavaScript     

底部一排縮圖、上方大圖跟著換,滑過就切、也會自動輪播——這是早期影片網站的標準焦點圖。核心是一個能補間位移的 moveElement 函式。

一、樣式

<style type="text/css">
* { margin:0; padding:0; word-break:break-all; }
body { background:#FFF; color:#333; font:12px/1.6em Helvetica, Arial, sans-serif; }
ul, li { list-style:none; }
em, strong, cite, th { font-style:normal; font-weight:normal; }

#focus_change {
  position:relative; width:450px; height:295px;
  overflow:hidden; margin:20px 0 1px 60px;
}
#focus_change_list { position:absolute; width:1800px; height:295px; }
#focus_change_list li { float:left; }
#focus_change_list li img { width:450px; height:295px; }

.focus_change_opacity {
  position:absolute; width:450px; height:70px;
  top:225px; left:0; background:#000;
  filter:alpha(opacity=50); -moz-opacity:0.5; opacity:0.5;
}
#focus_change_btn { position:absolute; width:450px; height:65px; top:225px; left:0; }
#focus_change_btn ul { padding-left:5px; }
#focus_change_btn li { display:inline; float:left; margin:0 15px; padding-top:12px; }
#focus_change_btn li img { width:76px; height:50px; border:2px solid #888; }
#focus_change_btn .current img { border-color:#EEE; }
</style>

二、核心:moveElement 補間函式

輪播的關鍵在於「把整排圖片向左推移」。四張 450px 寬的圖排成一列共 1800px,外層 overflow:hidden 只露出 450px,移動內層的 left 就能看到不同張。

function moveElement(elementID, final_x, final_y, interval) {
  var elem = document.getElementById(elementID);
  if (!elem) return false;

  if (elem.movement) clearTimeout(elem.movement);   // 避免多段動畫打架

  if (!elem.style.left) elem.style.left = "0px";
  if (!elem.style.top)  elem.style.top  = "0px";

  var xpos = parseInt(elem.style.left);
  var ypos = parseInt(elem.style.top);

  if (xpos == final_x && ypos == final_y) return true;   // 已到位,結束

  // 每次移動「剩餘距離的 1/10」(無條件進位)
  if (xpos < final_x) xpos += Math.ceil((final_x - xpos) / 10);
  if (xpos > final_x) xpos -= Math.ceil((xpos - final_x) / 10);
  if (ypos < final_y) ypos += Math.ceil((final_y - ypos) / 10);
  if (ypos > final_y) ypos -= Math.ceil((ypos - final_y) / 10);

  elem.style.left = xpos + "px";
  elem.style.top  = ypos + "px";

  // 用字串組出下一次呼叫,繼續遞迴
  var repeat = "moveElement('" + elementID + "'," + final_x + "," +
               final_y + "," + interval + ")";
  elem.movement = setTimeout(repeat, interval);
}

三、位移動畫的兩個關鍵技巧

1. 每步走剩餘距離的 1/10。這是「先快後慢」的簡易緩動。距離大時一次跳很多,越接近目標走得越少,視覺上就有減速感。

2. Math.ceil 保證收斂。若用 Math.round 或無條件捨去,當剩餘距離小於 10 時會算出 0,位置永遠到不了終點、動畫不會結束。Math.ceil 至少保證每步走 1px。

另外,把計時器 id 存在元素上(elem.movement)並在開頭 clear,是為了處理「動畫還沒跑完使用者又滑到別張」的情況——否則兩段動畫會互相搶同一個 style。

四、切換與自動輪播

function classNormal() {          // 清除所有縮圖的選中狀態
  var btns = $('focus_change_btn').getElementsByTagName('li');
  for (var i = 0; i < btns.length; i++) btns[i].className = '';
}

function focusChange() {         // 滑過縮圖 → 移動到對應位置
  var btns = $('focus_change_btn').getElementsByTagName('li');
  btns[0].onmouseover = function () {
    moveElement('focus_change_list', 0, 0, 5);
    classNormal(); btns[0].className = 'current';
  };
  btns[1].onmouseover = function () {
    moveElement('focus_change_list', -450, 0, 5);
    classNormal(); btns[1].className = 'current';
  };
  btns[2].onmouseover = function () {
    moveElement('focus_change_list', -900, 0, 5);
    classNormal(); btns[2].className = 'current';
  };
  btns[3].onmouseover = function () {
    moveElement('focus_change_list', -1350, 0, 5);
    classNormal(); btns[3].className = 'current';
  };
}

var atuokey = false;
function autoFocusChange() {     // 每 5 秒自動前進一張
  $('focus_change').onmouseover = function () { atuokey = true;  };  // 滑鼠移入暫停
  $('focus_change').onmouseout  = function () { atuokey = false; };
  if (atuokey) return;

  var btns = $('focus_change_btn').getElementsByTagName('li');
  var currentNum;
  for (var i = 0; i < btns.length; i++) {
    if (btns[i].className == 'current') currentNum = i;
  }
  var next = (currentNum + 1) % btns.length;
  moveElement('focus_change_list', -450 * next, 0, 5);
  classNormal();
  btns[next].className = 'current';
}
setInterval('autoFocusChange()', 5000);
window.onload = function () { focusChange(); };

自動輪播用 atuokey 旗標實作「滑鼠移入暫停」,避免使用者正在看某張時被強制切走。這個設計在今天的輪播元件裡依然是標準配備。

五、現代化建議

這份程式碼的邏輯完整,但可以用今天的方式大幅簡化:

  • 用 CSS transition 取代 moveElement。把 transition: transform .5s ease 掛在清單上,切換時只改 transform: translateX(-450px),交給合成器處理,比 JS 逐帧改 left 順得多。
  • 用 forEach 取代四段重複程式碼。索引直接乘上寬度:btns.forEach((b, i) => b.onmouseenter = () => go(i))。
  • 縮圖改用背景圖或 IntersectionObserver 懶載入。
  • 補上無障碍:自動輪播必須提供暫停按鈕(WCAG 2.2.2),縮圖要有 alt 文字,並在 prefers-reduced-motion 時停用自動播放。
  • 把 setInterval 字串改成函式參照,避免每次重新解析字串。
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved