效果需求
- 多張圖片水平無縫輪播。
- 底部圓點指示器,點擊/懸停切換對應圖片。
- 自動播放,鼠標進入暫停,離開恢復。
- 每張圖片帶標題/描述疊加層。
HTML 結構
<div id="slider" class="slider-container">
<div class="slides-wrapper">
<div class="slides">
<div class="slide">
<a href="#" target="_blank"><img src="img1.jpg" alt="Slide 1"></a>
<div class="caption"><h2>標題 1</h2><p>描述文字</p></div>
</div>
<div class="slide">...</div>
<div class="slide">...</div>
</div>
</div>
<ul class="indicators"></ul>
</div>
CSS 關鍵樣式
.slider-container { position: relative; width: 980px; height: 380px; margin: 0 auto; overflow: hidden; }
.slides-wrapper { position: relative; width: 100%; height: 100%; }
.slides { position: absolute; left: 0; top: 0; height: 100%; display: flex; transition: left 0.5s ease-out; }
.slide { flex: 0 0 100%; width: 980px; height: 380px; position: relative; }
.slide img { width: 100%; height: 100%; object-fit: cover; }
.caption { position: absolute; bottom: 0; left: 0; right: 0; padding: 20px; background: linear-gradient(transparent, rgba(0,0,0,.7)); color: #fff; }
.indicators { position: absolute; bottom: 15px; right: 15px; display: flex; gap: 8px; z-index: 10; }
.indicators li { width: 12px; height: 12px; border-radius: 50%; background: rgba(0,0,0,.5); cursor: pointer; transition: background .3s; }
.indicators li.active { background: #fff; }
jQuery 核心邏輯
(function($) {
$.fn.slider = function(options) {
const opts = $.extend({
autoPlay: true,
interval: 5000,
speed: 500,
pauseOnHover: true
}, options);
return this.each(function() {
const $container = $(this);
const $slides = $container.find('.slides');
const $slide = $container.find('.slide');
const $indicators = $container.find('.indicators');
const count = $slide.length;
const slideWidth = $slide.outerWidth(true); // 含 margin
let current = 0;
let timer = null;
// 初始化:設置寬度、生成指示器
$slides.css('width', count * slideWidth);
$slide.each(function(i) {
$indicators.append(`${i+1}`);
});
const $indicatorItems = $indicators.find('li');
$indicatorItems.eq(0).addClass('active');
// 切換函數
function goTo(index) {
if (index < 0) index = count - 1;
if (index >= count) index = 0;
$slides.stop(true, true).animate({ left: -index * slideWidth }, opts.speed);
$indicatorItems.removeClass('active').eq(index).addClass('active');
current = index;
}
// 自動播放
function startAuto() {
if (!opts.autoPlay) return;
timer = setInterval(() => goTo(current + 1), opts.interval);
}
function stopAuto() { clearInterval(timer); }
// 事件綁定
$indicators.on('click mouseenter', 'li', function() {
goTo($(this).data('index'));
});
if (opts.pauseOnHover) {
$container.hover(stopAuto, startAuto);
}
// 鍵盤左右鍵
$(document).on('keydown', e => {
if ($container.is(':hover')) {
if (e.key === 'ArrowLeft') goTo(current - 1);
else if (e.key === 'ArrowRight') goTo(current + 1);
}
});
startAuto();
});
};
})(jQuery);
// 初始化
$('#slider').slider({ interval: 5000, speed: 600 });
進階優化
- 無縫循環:複製首尾各一張圖片到兩端,動畫到複製項時瞬間跳回真實項(CSS
transition: none 瞬移)。
- 觸摸滑動:添加
touchstart/move/end 手勢識別,移動端拖拽切換。
- 懶加載:
<img data-src="...">,可視區內才設置 src。
- 響應式:容器
width: 100%; max-width: 980px;,圖片 width: 100%; height: auto;。