淘寶首頁焦點圖輪播是經典的「無縫循環 + 縮略圖指示器 + 懸停暫停 + 任意跳轉」綜合交互。本文通過原生 JS 還原核心邏輯,拆解關鍵難點:無縫循環原理、緩動動畫封裝、多定時器協作、事件解綁防洩漏,提供可直接落地的完整代碼。
* { margin:0; padding:0; }
img { border:none; width:470px; height:150px; float:left; }
.pic_body li { width:470px; height:150px; float:left; }
#div1 { width:470px; height:150px; position:relative; margin:100px auto; overflow:hidden; border:1px solid #000; }
#div1 ol { position:absolute; bottom:10px; right:10px; z-index:9999; }
#div1 ol li { float:left; margin-right:3px; padding:3px 4px; background:#ff0; border:1px solid #000; cursor:pointer; opacity:0.6; font:12px Arial; }
#div1 ol li.active { background:#ff0; font-weight:bold; padding:4px 6px; opacity:1; }
<div id="div1">
<ol><li class="active">1</li><li>2</li><li>3</li><li>4</li><li>5</li></ol>
<ul class="pic_body">
<li><img src="image/1.jpg"></li>
<li><img src="image/2.jpg"></li>
<li><img src="image/3.jpg"></li>
<li><img src="image/4.jpg"></li>
<li><img src="image/5.jpg"></li>
</ul>
</div>
window.onload = function() {
var now = 0; // 當前顯示索引
var oDiv = document.getElementById("div1");
var oOl = oDiv.getElementsByTagName("ol")[0];
var aBtn = oOl.getElementsByTagName("li");
var oUl = getClass(oDiv, "pic_body")[0]; // 獲取 ul
// 1. 克隆首張到末尾,實現無縫循環
var firstLi = oUl.children[0].cloneNode(true);
oUl.appendChild(firstLi); // 現在有 6 個 li,最後一個是第1張的克隆
// 2. 圓點點擊跳轉
for (var i = 0; i < aBtn.length; i++) {
aBtn[i].index = i;
aBtn[i].onclick = function() {
now = this.index;
tab();
};
}
// 3. 核心切換函數
function tab() {
for (var i = 0; i < aBtn.length; i++) aBtn[i].className = "";
aBtn[now].className = "active";
// 動畫移動 ul 的 top (垂直) 或 left (水平),此處演示垂直
startMove(oUl, { top: -150 * now });
}
// 4. 自動播放定時器
function next() {
now++;
if (now === aBtn.length) { // 到達克隆項 (索引5)
now = 0;
// 關鍵:無動畫瞬移回真實第一張 (top:0)
oUl.style.top = 0;
}
tab();
}
var timer = setInterval(next, 3000);
// 5. 懸停暫停/恢復
oUl.onmouseover = function() { clearInterval(timer); };
oUl.onmouseout = function() { timer = setInterval(next, 3000); };
};
// 獲取樣式兼容封裝
function getStyle(obj, attr) {
return obj.currentStyle ? obj.currentStyle[attr] : getComputedStyle(obj, false)[attr];
}
// 通用緩動移動:支持多屬性同時動畫
function startMove(obj, json, fnEnd) {
clearInterval(obj.timer);
obj.timer = setInterval(function() {
var bStop = true;
for (var attr in json) {
var cur = 0;
if (attr === "opacity") {
cur = Math.round(parseFloat(getStyle(obj, attr)) * 100);
} else {
cur = parseInt(getStyle(obj, attr));
}
var speed = (json[attr] - cur) / 8; // 緩動係數,越大越快
speed = speed > 0 ? Math.ceil(speed) : Math.floor(speed);
if (cur !== json[attr]) bStop = false;
if (attr === "opacity") {
obj.style.opacity = (cur + speed) / 100;
obj.style.filter = "alpha(opacity:" + (cur + speed) + ")";
} else {
obj.style[attr] = cur + speed + "px";
}
}
if (bStop) {
clearInterval(obj.timer);
if (fnEnd) fnEnd();
}
}, 30);
}
// 通過 class 獲取元素(兼容舊瀏覽器)
function getClass(oParent, sClass) {
var aResult = [];
var aEle = oParent.getElementsByTagName("*");
for (var i = 0; i < aEle.length; i++) {
if (aEle[i].className === sClass) aResult.push(aEle[i]);
}
return aResult;
}
索引: 0 1 2 3 4 5(克隆0)
圖片: [圖1] [圖2] [圖3] [圖4] [圖5] [圖1']
top: 0 -150 -300 -450 -600 -750
正常播放: 0 → 1 → 2 → 3 → 4
關鍵時刻: now=5 時,瞬移 top=0 (顯示真實圖1),now 重置為 0,用戶無感知
下一輪: 0 → 1 → 2 ... 循環
將 top 改為 left,150 改為圖片寬度 470,pic_body li 改為 float:left,ul 寬度設為 li數量 * 470,其余邏輯完全一致。
setInterval 動畫循環,幀率更穩。ul { transition: transform .5s ease; },JS 僅切換 transform: translateX(-470px * index),性能最優、代碼最少。touchstart/move/end 實現移動端拖拽切換。class Carousel 或 Vue/React 組件,暴露 next/prev/go/index API。這份原生實現雖代碼量較大,但徹底暴露了輪播的所有內部細節,是理解「無縫循環、緩動算法、定時器管理」的最佳教材。生產環境建議直接使用 Swiper(功能最全)、Vue Carousel、React Swiper 等成熟方案。