圖片輪播 (Carousel/Slider) 是電商 Banner、新聞焦點圖、產品展示的核心組件。本文實現一個原生 JS 輪播組件:支持標題/描述、數字導航、自動播放、鼠標懸停切換、點擊跳轉,兼容 IE6+。
<div class="slider-container">
<!-- 輪播圖片區域 -->
<img src="ad-01.jpg" width="524" height="190" border="0" alt="廣告圖片" id="sliderImg">
<!-- 導航標籤區域 -->
<div class="slider-nav">
<div class="nav-item" id="nav1" onclick="show(1)">1</div>
<div class="nav-item" id="nav2" onclick="show(2)">2</div>
<div class="nav-item" id="nav3" onclick="show(3)">3</div>
<div class="nav-item" id="nav4" onclick="show(4)">4</div>
</div>
</div>
.slider-container {
width: 524px; border: 1px solid #666; margin: 20px auto; position: relative;
font-family: "Microsoft YaHei", sans-serif;
}
.slider-nav {
background: #E0E0E0; height: 30px; line-height: 30px;
border-top: 1px solid #B4B4B4; padding: 5px 0 5px 10px;
}
.nav-item {
display: inline-block; width: 24px; height: 24px; line-height: 24px;
text-align: center; font-size: 14px; font-weight: bold; color: #FFF;
background: #9E2E07; border: 1px solid #FFF; margin-right: 8px;
cursor: pointer; border-radius: 2px;
}
.nav-item.active {
background: #FFF; color: #8C2806; width: auto; padding: 0 12px;
border-color: #ccc;
}
/* CSS */
#sliderImg { transition: opacity 0.5s ease; }
.fade-out { opacity: 0; }
.fade-in { opacity: 1; }
/* JS 切換時 */
imgEl.classList.add('fade-out');
setTimeout(() => {
imgEl.src = slides[currentIndex].img;
imgEl.classList.replace('fade-out', 'fade-in');
}, 200);
.slider-container { max-width: 100%; }
#sliderImg { width: 100%; height: auto; display: block; }
var startX = 0;
document.querySelector('.slider-container').addEventListener('touchstart', e => startX = e.touches[0].clientX);
document.querySelector('.slider-container').addEventListener('touchend', e => {
var dx = e.changedTouches[0].clientX - startX;
if (Math.abs(dx) > 50) {
stopAutoPlay();
show(currentIndex + (dx > 0 ? -1 : 1)); // 右滑上一張,左滑下一張
}
});
// 思路:複製首尾節點,滾動到複製項時瞬間跳回真實項
// 結構: [最後] [1] [2] [3] [4] [第一]
// CSS: container { overflow: hidden; white-space: nowrap; }
// JS: transform: translateX() 動畫
<style>
.body{ width:524px; border:solid 1px #666; margin:auto; }
.bg{ background:#E0E0E0; height:20px; border-top:solid 1px #B4B4B4; }
.number{ font-size:14px; font-weight:bold; color:#FFF; background:#9E2E07; display:block; border:1px solid #FFF; width:18px; height:18px; text-align:center; margin-left:10px; cursor:pointer; float:left; }
.numberOver{ color:#8C2806; width:280px; background:#FFF; text-align:center; float:left; display:block; margin-left:10px; }
</style>
<div class="body">
<img src="ad-01.jpg" width="524" height="190" id="Rotator">
<div class="bg">
<div class="number" id="fig_1" onclick="show(1)">1</div>
<div class="number" id="fig_2" onclick="show(2)">2</div>
<div class="number" id="fig_3" onclick="show(3)">3</div>
<div class="number" id="fig_4" onclick="show(4)">4</div>
</div>
</div>
<script>
var title = [
"1.店慶第一波 限時搶購 一日三瘋!",
"2.十年店慶均價場 39/99/169專場!",
"3.全場69折封頂 享當當的超值低價!",
"4.店慶钜獻 海量圖書69折封頂"
];
var NowFrame = 1, MaxFrame = 4, theTimer;
function show(d1) {
if (Number(d1)) { clearTimeout(theTimer); NowFrame = d1; }
for (var i=1; i<=MaxFrame; i++) {
if (i == NowFrame) {
document.getElementById("Rotator").src = "ad-0"+i+".jpg";
document.getElementById("fig_"+i).innerHTML = title[i-1];
document.getElementById("fig_"+i).className = "numberOver";
} else {
document.getElementById("fig_"+i).innerHTML = i;
document.getElementById("fig_"+i).className = "number";
}
}
NowFrame = (NowFrame % MaxFrame) + 1;
}
theTimer = setInterval('show()', 3000);
window.onload = show;
</script>