<div id="slider" class="slider">
<ul class="slides">
<li class="current"><img src="img/1.jpg" alt="Slide 1"></li>
<li><img src="img/2.jpg" alt="Slide 2"></li>
<li><img src="img/3.jpg" alt="Slide 3"></li>
<li><img src="img/4.jpg" alt="Slide 4"></li>
<li><img src="img/5.jpg" alt="Slide 5"></li>
</ul>
<ul class="indicators">
<li class="current">1</li>
<li>2</li>
<li>3</li>
<li>4</li>
<li>5</li>
</ul>
</div>
.slider {
position: relative;
width: 600px; height: 350px; /* 依需求調整 */
margin: 20px auto;
border-radius: 8px;
overflow: hidden;
box-shadow: 0 4px 15px rgba(0,0,0,0.1);
background: #000;
}
.slides, .indicators { margin: 0; padding: 0; list-style: none; }
.slides {
position: relative;
width: 100%; height: 100%;
}
.slides li {
position: absolute;
top: 0; left: 0;
width: 100%; height: 100%;
opacity: 0;
transition: opacity 0.6s ease; /* 核心:CSS Transition 實現淡入淡出 */
}
.slides li.current { opacity: 1; z-index: 1; }
.slides img { width: 100%; height: 100%; object-fit: cover; display: block; }
.indicators {
position: absolute;
bottom: 15px; right: 15px;
display: flex; gap: 8px;
z-index: 2;
}
.indicators li {
width: 24px; height: 24px;
line-height: 24px; text-align: center;
background: rgba(255,255,255,0.5);
color: #333; font-size: 12px;
border-radius: 50%; cursor: pointer;
transition: all 0.3s;
}
.indicators li.current {
background: #fff; color: #000; font-weight: bold;
transform: scale(1.1);
}
const Slider = (function() {
let timer = null; // 自動播放計時器
let fadeTimer = null; // 淡入動畫計時器 (若用 JS 動畫)
let index = 0; // 目前顯示索引
function init(containerSelector) {
const box = document.querySelector(containerSelector);
if (!box) return;
const slides = box.querySelectorAll(".slides li");
const indicators = box.querySelectorAll(".indicators li");
const total = slides.length;
if (total <= 1) return;
// 1. 綁定指示器點擊/懸停事件
indicators.forEach((dot, i) => {
dot.addEventListener("mouseenter", () => switchTo(i));
});
// 2. 滑鼠進入/離開控制自動播放
box.addEventListener("mouseenter", stopAuto);
box.addEventListener("mouseleave", startAuto);
// 3. 核心切換函式
function switchTo(newIndex) {
if (newIndex === index) return;
index = (newIndex + total) % total;
// 更新指示器狀態
indicators.forEach((dot, i) => dot.classList.toggle("current", i === index));
// 利用 CSS Transition:只需切換 class,瀏覽器自動處理淡入淡出
slides.forEach((slide, i) => slide.classList.toggle("current", i === index));
}
// 4. 自動播放控制
function next() { switchTo(index + 1); }
function startAuto() { stopAuto(); timer = setInterval(next, 3000); }
function stopAuto() { clearInterval(timer); timer = null; }
// 5. 啟動
startAuto();
}
return { init };
})();
// 頁面載入後初始化
document.addEventListener("DOMContentLoaded", () => Slider.init("#slider"));
原始範例使用 setInterval 手動遞增 opacity,現代做法**強烈建議改用 CSS Transition**:
class,無需手動管理計時器與數值插值。setInterval/setTimeout 必須配對 clearInterval/clearTimeout。destroy() 方法清理計時器與事件監聽器。role="button"、aria-label="切換至第 N 張"、tabindex="0"。alt 屬性。let touchStartX = 0;
box.addEventListener("touchstart", e => { touchStartX = e.changedTouches[0].clientX; }, { passive: true });
box.addEventListener("touchend", e => {
const dx = e.changedTouches[0].clientX - touchStartX;
if (Math.abs(dx) > 50) { // 閾值 50px
switchTo(index + (dx < 0 ? 1 : -1));
}
}, { passive: true });
淡入淡出輪播圖是前端入門經典案例。現代開發應善用 **CSS Transition** 處理視覺動畫,**JS 僅負責狀態切換與互動邏輯**,達成關注點分離。本文提供的模組化寫法可直接應用於實際專案,並易於擴充觸控、鍵盤導航、無限循環、縮圖預覽等進階功能。