<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>jQuery 幻燈片輪播</title>
<style>
* { margin: 0; padding: 0; box-sizing: border-box; }
ul, li { list-style: none; }
a { color: #333; text-decoration: none; }
.slider { width: 420px; margin: 20px auto; position: relative; overflow: hidden; }
.slider img { border: 0; width: 100%; height: auto; display: block; }
.slides { width: 100%; height: 196px; overflow: hidden; position: relative; }
.slides a { display: none; position: absolute; top: 0; left: 0; width: 100%; height: 100%; }
.slides a:first-child { display: block; } /* 初始顯示第一張 */
.slides span {
position: absolute; left: 0; bottom: 0; width: 100%; height: 30px;
line-height: 30px; text-indent: 1em; color: #fff; font-size: 13px; z-index: 11;
}
.slide-bg {
position: absolute; left: 0; bottom: 0; width: 100%; height: 30px;
background: #000; opacity: 0.5; z-index: 10;
}
.thumbs { width: 100%; margin-top: 2px; font-size: 0; } /* 消除 inline-block 間隙 */
.thumbs li {
display: inline-block; width: 25%; cursor: pointer;
border: 2px solid transparent; transition: border-color 0.3s;
}
.thumbs li.active { border-color: #e74c3c; }
.thumbs img { width: 100%; height: auto; display: block; }
</style>
</head>
<body>
<div class="slider">
<div class="slide-bg"></div>
<div class="slides">
<a href="#"><img src="https://picsum.photos/420/196?1"><span>標題一:風景</span></a>
<a href="#"><img src="https://picsum.photos/420/196?2"><span>標題二:建築</span></a>
<a href="#"><img src="https://picsum.photos/420/196?3"><span>標題三:人像</span></a>
<a href="#"><img src="https://picsum.photos/420/196?4"><span>標題四:抽象</span></a>
</div>
<ul class="thumbs">
<li class="active"><img src="https://picsum.photos/100/50?1"></li>
<li><img src="https://picsum.photos/100/50?2"></li>
<li><img src="https://picsum.photos/100/50?3"></li>
<li><img src="https://picsum.photos/100/50?4"></li>
</ul>
</div>
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(function() {
const $slides = $('.slides a');
const $thumbs = $('.thumbs li');
const COUNT = $slides.length;
let index = 0;
let timer = null;
const INTERVAL = 3000;
// 切換到指定索引
function slideTo(i) {
index = (i + COUNT) % COUNT; // 循環
$slides.stop(true, true).eq(index).fadeIn(400).siblings().fadeOut(400);
$thumbs.removeClass('active').eq(index).addClass('active');
}
// 自動播放
function autoPlay() {
timer = setInterval(() => slideTo(index + 1), INTERVAL);
}
// 停止自動播放
function stopAuto() {
clearInterval(timer);
timer = null;
}
// 綁定事件
$('.slides').hover(stopAuto, autoPlay); // 大圖區域
$('.thumbs').hover(stopAuto, autoPlay); // 縮圖區域
$thumbs.hover(function() {
stopAuto();
slideTo($(this).index());
}, function() {
// 滑出縮圖區域恢復自動播放 (由外層 hover 處理)
});
// 初始啟動
autoPlay();
});
</script>
</body>
</html>
| 功能 | 實作關鍵 |
|---|---|
| 淡入淡出 | .fadeIn().siblings().fadeOut() 配合 position: absolute 疊放 |
| 停止動畫佇列 | .stop(true, true) 清除佇列並跳至終點,防止快速切換疊加 |
| 循環索引 | (i + COUNT) % COUNT 處理正向/反向循環 |
| 計時器管理 | 統一 timer 變數,hover 進/出控制 clearInterval/setInterval |
| 縮圖同步 | .eq(index).addClass('active').siblings().removeClass('active') |
max-width: 100% + height: auto 讓容器自適應,JS 取寬度計算。hammer.js 或原生 touchstart/move/end 實現左右滑動切換。data-src,切換時才設定 src。role="region" aria-roledescription="輪播"、鍵盤左右鍵控制、aria-live 播報。$.fn.slider() 或 Vue/React 元件。若專案允許引入庫,推薦:
本範例展示了原生 jQuery 實現功能完整幻燈片的核心邏輯:狀態機 (index/timer)、事件協調 (hover/click)、動畫控制。理解這些基礎有助於除錯第三方庫、客製化特殊需求、或在無法引入大型庫的環境下快速實作。