在 Flash 逐漸退場的那幾年,「仿 Flash 輪播」是每個首頁 Banner 的標準配備。這個 blockSlide 插件用純 jQuery 就做到了淡入淡出式的圖片輪播,程式碼不到四十行,非常適合當作動畫排程的入門練習。
整個輪播區由一個外層容器包著,內層放幾張疊在一起的圖,最後一個 <div id="flow"> 是留給指示器(小方塊)的容器:
<div id="imgADPlayer">
<div id="AdTop">
<div id="myjQueryContent">
<div><a href="javascript:void(0)"><img src="_Template/images/photo_01.jpg" alt="" /></a></div>
<div class="smask"><a href="javascript:void(0)"><img src="_Template/images/photo_02.jpg" alt="" /></a></div>
<div class="smask"><a href="javascript:void(0)"><img src="_Template/images/photo_03.jpg" alt="" /></a></div>
<div class="smask"><a href="javascript:void(0)"><img src="_Template/images/photo_04.jpg" alt="" /></a></div>
</div>
<div id="flow"></div>
</div>
</div>
樣式的重點在於所有圖片都必須絕對定位並疊在同一個位置,否則淡入淡出時會看到版面跳動:
div#imgADPlayer
{
margin: auto;
margin-bottom: 4px;
width: 960px;
height: 120px;
background: url(../images/photo_01.jpg) left top no-repeat;
padding: 0px;
border: none;
clear: both;
position: relative; /* 作為內部絕對定位的參照 */
}
div#imgADPlayer .smask
{
position: absolute;
left: 0px;
top: 0px;
}
呼叫方式只需四個參數:
<script type="text/javascript">
$(document).ready(function () {
$("#AdTop").blockSlide({
speed: "normal", // 圖片輪播(淡入淡出)速度
num: 4, // 圖片數量
timer: 3000, // 自動輪播的間隔毫秒數
flowSpeed: 300 // 指示器滑塊移動速度
});
});
</script>
/**
* @author huajianhuakai
*/
(function($)
{
$.fn.blockSlide = function(settings)
{
settings = jQuery.extend({
speed: "normal",
num: 4,
timer: 1000,
flowSpeed: 300
}, settings);
return this.each(function()
{
$.fn.blockSlide.scllor($(this), settings);
});
};
$.fn.blockSlide.scllor = function($this, settings)
{
var index = 0;
var imgScllor = $("div:eq(0)>div", $this); // 取出所有圖片層
var timerID;
// 自動播放
var MyTime = setInterval(function()
{
ShowjQueryFlash(index);
index++;
if (index == settings.num) { index = 0; }
}, settings.timer);
var ShowjQueryFlash = function(i)
{
$(imgScllor).eq(i)
.animate({ opacity: 1 }, settings.speed)
.css({ "z-index": "100" })
.siblings()
.animate({ opacity: 0 }, settings.speed)
.css({ "z-index": "0" });
};
}
})(jQuery);
核心邏輯只有一句:把目標圖的 opacity 動畫到 1 並拉高 z-index,同時把它的兄弟元素動畫到 0 並壓低 z-index。z-index 的切換是為了確保淡入的那張永遠在上層,不會被尚未淡出的舊圖遮住。
作為教學範例很勝任,但直接用於正式專案前,建議補上以下幾點。
一、setInterval 從未清除。timerID 與 MyTime 宣告了卻沒被使用,計時器永遠在跑。滑鼠移入時應該 clearInterval 暫停(否則使用者還在看第一張,畫面已經自己跳走了),移出後重啟。
二、沒有指示器與切換按鈕。#flow 容器留著卻沒有程式填入內容,flowSpeed 參數也從未被使用。使用者被跳過的圖片吸引時,沒有任何方式回到前一張。
三、num 需手動維護。圖片數量寫死在參數裡,日後增刪圖片就得同步改 JS。改用 $(imgScllor).length 自動取得即可。
四、無障礙完全缺席。輪播圖對螢幕閱讀器而言只是一疊 <img>;自動播放無法暫停也違反 WCAG 2.2.2。應為容器加上 role="region" 與 aria-label、為非目前顯示的圖片設 aria-hidden="true",並提供明確的暫停控制。
把這些補齊後的骨架大致如下:
var idx = 0, timer = null, total = $imgs.length;
function show(i){
$imgs.eq(i).stop(true).animate({opacity:1}, speed).css("z-index", 100)
.siblings().stop(true).animate({opacity:0}, speed).css("z-index", 0);
$dots.removeClass("on").eq(i).addClass("on")
.attr("aria-current", i === 0 ? "true" : "false");
$imgs.attr("aria-hidden", function(n){ return n === i ? "false" : "true"; });
}
function play(){ timer = setInterval(function(){ show(idx = (idx + 1) % total); }, delay); }
function pause(){ clearInterval(timer); }
$wrap.on("mouseenter focusin", pause).on("mouseleave focusout", play);
$("#prev").click(function(){ show(idx = (idx - 1 + total) % total); });
$("#next").click(function(){ show(idx = (idx + 1) % total); });
play();
順帶一提,.stop(true) 是必要的——連續快速點擊時若不加,動畫佇列會堆積,畫面就會出現「點了五下、圖片慢慢輪播五次」的怪現象。