DIV CSS 佈局教程網

打造簡潔好用的自動播放幻燈片
編輯:JQuery特效代碼     

自動輪播圖片是當今網站常見的展示手法,網路上特效繽紛的幻燈片外掛與程式碼不勝枚舉。這裡因應專案需求,自己動手做了一個最簡單的版本,邏輯清爽、易於理解。直接看效果與程式碼吧。

效果如下圖:

所有程式碼都在一個 ppt.html 中(需相應圖片才能顯示):

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "https://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<script src="https://libs.baidu.com/jquery/1.9.0/jquery.js"></script>
<title>PPT Demo</title>
<style type="text/css">
.ppt-container, .ppt-container ul, .ppt-container li, .ppt-container img {
    margin: 0; padding: 0; border: 0;
}
.ppt-container {
    width: 752px;  /* 根據展示圖片大小設置容器 width 和 height */
    height: 328px;
    position: relative;
}
.ppt-container img { width: 100%; height: 100%; }
.ppt-container .hide { display: none; }
.ppt-container ul.image-list li { position: absolute; top: 0px; left: 0px; }
.ppt-container ul.button-list {
    list-style: none; position: absolute; right: 20px; bottom: 20px;
}
.ppt-container ul.button-list li { float: left; padding-right: 10px; }
.ppt-container ul.button-list span {
    background: #E5E5E5; padding: 1px 7px; line-height: 20px;
    font-size: 13px; display: block; cursor: default;
}
.ppt-container ul.button-list span.selected { color: #FFF; background: #FF7000; }
</style>
<script type="text/javascript">
$(function() {
    var iCountOfImage = 3; // 共三張圖片
    var iPreIndex = 0;     // 上一次索引位置
    $(".ppt-container ul.button-list li span").click(function() {
        var iIndex = $(this).attr("data-index");
        if(iIndex == iPreIndex) {
            return; // 點擊了當前圖片,不切換
        }
        $(".ppt-container .image-list li[data-index=" + iIndex + "]").fadeIn(1500);
        $(".ppt-container .image-list li[data-index=" + iPreIndex + "]").fadeOut(1500);
        iPreIndex = iIndex;
        $(".ppt-container .button-list span").removeClass("selected");
        $(this).addClass("selected");
    });
    setInterval(function() { // 自動播放,每 5 秒觸發一次單擊事件
        var iNextIndex = (iPreIndex + 1) % iCountOfImage;
        $(".ppt-container ul.button-list li span[data-index=" + iNextIndex + "]").click();
    }, 5000);
});
</script>
</head>
<body>
<div class="ppt-container">
<ul class="image-list">
<li data-index="0"><img src="ppt-images/ppt-1.jpg"/></li>
<li data-index="1" class="hide"><img src="ppt-images/ppt-2.jpg"/></li>
<li data-index="2" class="hide"><img src="ppt-images/ppt-3.jpg"/></li>
</ul>
<ul class="button-list">
<li><span data-index="0" class="selected">1</span></li>
<li><span data-index="1">2</span></li>
<li><span data-index="2">3</span></li>
</ul>
</div>
</body>
</html>

這個幻燈片的巧妙之處在於「以點擊驅動輪播」。每张圖與每個數字按鈕都帶一個 data-index 標記彼此對應;點擊按鈕時,用 fadeIn 淡入目標圖、fadeOut 淡出前一張,並切換按鈕的 selected 樣式。而自動播放則靠 setInterval 每五秒「模擬點擊」下一個按鈕,用取餘運算 (iPreIndex + 1) % iCountOfImage 讓索引循環回到開頭。這種把「自動輪播」簡化為「定時觸發點擊」的思路,讓整份程式碼既好讀又好改。你只要替換圖片路徑、調整容器尺寸與輪播間隔,就能立刻用在專案裡。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved