凡客誠品 (Vancl) 絲襪頻道頁面底部呈現一組商品圖片輪播,獨特之處在於:
var __INFO__ = {
total: 10, // 圖片總數,決定滑塊寬度與捲動範圍
skinid: 0 // 樣式主題 ID
};
// 軌道寬度 = 容器寬度 - 兩側邊距
var trackWidth = containerWidth - 20;
// 滑塊寬度 = 軌道寬度 * (可視數量 / 總數量)
// 可視數量通常為 4-5 張,總數量 = total
var thumbWidth = Math.max(30, trackWidth * (visibleCount / total));
// 最大捲動距離 = 軌道寬度 - 滑塊寬度
var maxScroll = trackWidth - thumbWidth;
// 內容捲動比例 = 內容總寬度 / 可視寬度
// 滑塊位置 = 內容 scrollLeft / 內容最大 scrollLeft * 最大捲動距離
// 1. 內容捲動 → 更新捲軸 (監聽內容區 scroll 事件)
contentArea.addEventListener('scroll', function() {
var ratio = this.scrollLeft / (this.scrollWidth - this.clientWidth);
thumb.style.left = ratio * maxScroll + 'px';
});
// 2. 拖曳捲軸 → 控制內容捲動
thumb.addEventListener('mousedown', function(e) {
var startX = e.clientX;
var startLeft = parseInt(thumb.style.left) || 0;
function move(e) {
var dx = e.clientX - startX;
var newLeft = Math.max(0, Math.min(maxScroll, startLeft + dx));
thumb.style.left = newLeft + 'px';
// 同步內容
var ratio = newLeft / maxScroll;
contentArea.scrollLeft = ratio * (contentArea.scrollWidth - contentArea.clientWidth);
}
function up() {
document.removeEventListener('mousemove', move);
document.removeEventListener('mouseup', up);
}
document.addEventListener('mousemove', move);
document.addEventListener('mouseup', up);
e.preventDefault();
});
// 3. 點擊軌道跳轉
track.addEventListener('click', function(e) {
if (e.target === thumb) return; // 避免與拖曳衝突
var clickX = e.offsetX;
var newLeft = Math.max(0, Math.min(maxScroll, clickX - thumbWidth/2));
thumb.style.left = newLeft + 'px';
contentArea.scrollLeft = (newLeft / maxScroll) * (contentArea.scrollWidth - contentArea.clientWidth);
});
<div class="vancl-slider">
<div class="viewport">
<div class="content" id="sliderContent">
<a href="#"><img src="1.jpg"></a>
<a href="#"><img src="2.jpg"></a>
...
</div>
</div>
<div class="scrollbar">
<div class="track" id="scrollTrack">
<div class="thumb" id="scrollThumb"></div>
</div>
</div>
</div>
<style>
.vancl-slider { position: relative; width: 980px; margin: 0 auto; }
.viewport { overflow: hidden; white-space: nowrap; }
.content { display: inline-block; white-space: nowrap; font-size: 0; transition: transform 0.3s; }
.content a { display: inline-block; width: 220px; margin-right: 20px; }
.scrollbar { height: 16px; margin-top: 10px; position: relative; }
.track { height: 6px; background: #ddd; border-radius: 3px; position: relative; }
.thumb { position: absolute; top: -5px; width: 60px; height: 16px; background: #e74c3c; border-radius: 8px; cursor: pointer; }
</style>
min-width: 30px。tabindex="0" 讓捲軸可聚焦,左右鍵控制 scrollLeft。touchstart/move/end 同步邏輯,或直接讓內容區 overflow-x: auto 配合 -webkit-overflow-scrolling: touch 原生滑動,捲軸僅作視覺指示。requestAnimationFrame 節流 scroll 事件,或改用 IntersectionObserver 偵測可見項。若不需高度客製化捲軸外觀,建議直接使用 CSS Scroll Snap + 原生捲軸:
.viewport { overflow-x: auto; scroll-snap-type: x mandatory; -webkit-overflow-scrolling: touch; }
.content { display: flex; gap: 20px; }
.content a { flex: 0 0 220px; scroll-snap-align: start; }
瀏覽器原生支援捲動捕捉、動量滾動、觸控優化,零 JS 依賴。再配合 ::-webkit-scrollbar 美化捲軸外觀即可達到類似效果。