多張圖片疊加,點擊當前圖片向左翻轉/滑動顯示下一張,類似翻書或卡片輪播。支援自動播放、導航點、鍵盤操作、觸控滑動。
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>圖片翻頁展示</title>
<script src="https://cdn.jsdelivr.net/npm/jquery@3.7.1/dist/jquery.min.js"></script>
<style>
.flip-book { perspective: 1000px; width: 400px; height: 300px; margin: 50px auto; position: relative; }
.page {
position: absolute; top: 0; left: 0; width: 100%; height: 100%;
transform-style: preserve-3d; transition: transform 0.8s cubic-bezier(0.4, 0, 0.2, 1);
transform-origin: left center; cursor: pointer;
backface-visibility: hidden;
}
.page img { width: 100%; height: 100%; object-fit: cover; border-radius: 8px; display: block; }
.page.front { z-index: 2; } /* 當前顯示 */
.page.back { z-index: 1; transform: rotateY(-180deg); } /* 背面 */
/* 導航點 */
.dots { display: flex; justify-content: center; gap: 8px; margin-top: 16px; }
.dot { width: 10px; height: 10px; border-radius: 50%; background: #ccc; cursor: pointer; transition: background 0.2s; }
.dot.active { background: #1890ff; }
</style>
</head>
<body>
<div class="flip-book" id="flipBook">
<div class="page front" data-index="0"><img src="images/m01.jpg" alt="頁面 1"></div>
<div class="page back" data-index="1"><img src="images/m02.jpg" alt="頁面 2"></div>
<div class="page" data-index="2"><img src="images/m03.jpg" alt="頁面 3"></div>
<div class="page" data-index="3"><img src="images/m04.jpg" alt="頁面 4"></div>
</div>
<div class="dots" id="dots"></div>
<script>
(($) => {
const $book = $('#flipBook');
const $pages = $book.children('.page');
const $dots = $('#dots');
const total = $pages.length;
let current = 0;
let animating = false;
let autoTimer = null;
// 初始化導航點
$pages.each((i) => {
$('').toggleClass('active', i === 0).appendTo($dots).click(() => goTo(i));
});
function updateZIndex() {
$pages.each((i, el) => {
const $el = $(el);
const diff = (i - current + total) % total;
if (diff === 0) { $el.css('z-index', 3).addClass('front').removeClass('back'); }
else if (diff === 1) { $el.css('z-index', 2).addClass('back').removeClass('front'); }
else { $el.css('z-index', 1).removeClass('front back'); }
});
$dots.children().removeClass('active').eq(current).addClass('active');
}
function flipTo(next) {
if (animating || next === current) return;
animating = true;
const direction = (next - current + total) % total === 1 ? 'next' : 'prev'; // 簡化:只支援順序翻頁
// 這裡簡化為循環順序翻頁,實際可計算最短路徑
current = next;
updateZIndex();
// 觸發翻轉動畫:當前 front 旋轉 -180deg,下一張 back 旋轉 0
const $currFront = $pages.filter('.front');
const $nextBack = $pages.filter('.back');
$currFront.css('transform', 'rotateY(-180deg)');
$nextBack.css('transform', 'rotateY(0deg)');
setTimeout(() => {
$currFront.removeClass('front').addClass('back');
$nextBack.removeClass('back').addClass('front');
animating = false;
}, 800);
}
function next() { flipTo((current + 1) % total); }
function prev() { flipTo((current - 1 + total) % total); }
function goTo(i) { flipTo(i); }
// 點擊當前頁翻下一頁
$book.on('click', '.front', next);
// 鍵盤
$(document).on('keydown', (e) => {
if (e.key === 'ArrowRight') next();
else if (e.key === 'ArrowLeft') prev();
});
// 自動播放
function startAuto() { autoTimer = setInterval(next, 5000); }
function stopAuto() { clearInterval(autoTimer); }
$book.hover(stopAuto, startAuto);
startAuto();
// 觸控滑動
let startX = 0;
$book.on('touchstart', (e) => { startX = e.originalEvent.touches[0].clientX; stopAuto(); });
$book.on('touchend', (e) => {
const dx = e.originalEvent.changedTouches[0].clientX - startX;
if (Math.abs(dx) > 50) dx < 0 ? next() : prev();
startAuto();
});
updateZIndex();
})(jQuery);
</script>
</body>
</html>
<script src="https://cdn.jsdelivr.net/npm/swiper@11/swiper-bundle.min.js"></script>
<script>
new Swiper('.flip-book', {
effect: 'flip', // 或 'cube', 'coverflow', 'cards'
grabCursor: true,
loop: true,
autoplay: { delay: 5000 },
pagination: { el: '.dots', clickable: true, bulletClass: 'dot', bulletActiveClass: 'active' },
on: { slideChange: () => { /* 可選回調 */ } }
});
</script>
Swiper 內建多種 3D 效果、觸控優化、無障礙支援,生產環境強烈推薦使用成熟庫而非手寫。