前陣子做一個網站,需要一個尺寸大一點的幻燈片。網上一搜一大堆,本來想直接「淘」一個省點工夫,結果看了好些都不滿意——不是造型老土不美觀,就是跨瀏覽器有問題,要不就是做不到想要的結果。最後決定自己寫一個。
XHTML 部分:
<div id="slide">
<a href="#" id="this_a"><img src="images/1.jpg" id="this_pic" /></a>
<ul id="pic_list">
<li><a href="https://www.qq.com"><img src="images/1.jpg" alt="" /></a></li>
<li><a href="https://www.sina.com"><img src="images/2.jpg" alt="" /></a></li>
<li><a href="https://www.163.com"><img src="images/3.jpg" alt="" /></a></li>
<li><a href="https://www.baidu.com"><img src="images/4.jpg" alt="" /></a></li>
<li><a href="https://www.17css.com"><img src="images/5.jpg" alt="" /></a></li>
</ul>
<span id="transparence"></span>
</div>
結構說穿了很簡單:#slide 是最外層容器,#this_pic 是顯示用的大圖,#pic_list 是縮圖清單,而 #transparence 是一層半透明遮罩。之所以要多這一層,是因為直接對 #pic_list 設透明度會被子元素繼承、無法各自調整,所以乾脆鋪一層半透明黑 ourselves 來模擬。
#slide {position:relative; width:740px; height:240px; margin:20px auto;}
#pic_list { position:absolute; left:0; bottom:0; width:100%;
list-style: none; overflow:hidden; z-index:2;}
#pic_list li { float:left; width:100px; height:52px; padding-top:10px;}
#pic_list li img { position:absolute; top:18px; width:65px; height:35px;
margin:0 20px; border:1px solid #fff;}
#this_pic { position:absolute; width:100%; height:100%; border:none;}
#transparence { position:absolute; left:0; bottom:0; width:100%; height:50px;
background:#000; z-index:1; border-top:1px solid #fff;}
$(function (){
$('#pic_list img').mouseover(function(){
// 如果移到的縮圖與目前大圖相同,直接返回
if($('#this_pic').attr('src') == $(this).attr('src')) return;
// 大圖淡出再淡入,並換上新的 src
$('#this_pic').fadeOut(0).fadeIn(500).attr('src', $(this).attr('src'));
// 把縮圖的連結位址套到大圖的連結上
$('#this_a').attr('href', $(this).parents('a').attr('href'));
// 其餘縮圖還原位置與半透明
$(this).parents('li').nextAll('li').find('img').animate({top:18, opacity:0.6}, 500);
$(this).parents('li').prevAll('li').find('img').animate({top:18, opacity:0.6}, 500);
// 當前縮圖上浮並變為不透明
$(this).animate({top:0}, 500).css('opacity','1');
});
});
// 初始化
$(function(){
$('#transparence').css('opacity','0.4'); // 遮罩層半透明
$('#pic_list img').css({'opacity':'0.6'}); // 所有縮圖半透明
$('#pic_list img:eq(0)').css({'top':'0','opacity':'1'}); // 第一張為啟始狀態
$('#pic_list a').click(function(){ return false }); // 攔下縮圖連結
});
幾個設計上的小心思:nextAll() 與 prevAll() 一組搭配,就能把「除了當前之外的所有縮圖」還原,不必再寫一次完整的迴圈;開頭那個 src 比對則避免滑在同一張縮圖上時重複播放動畫。
半透明的程式碼刻意放在 JavaScript 而非 CSS,一方面是為了通過驗證——CSS 的 opacity 屬性在當時的驗證器眼中還不合法,而且為了相容各瀏覽器得寫三句不同寫法,實在不好看。放在 JS 裡一次 css() 呼叫就解決了。
今天的做法會很不一樣:CSS 的 opacity 早已是標準且所有瀏覽器都支援,直接寫在樣式表裡就好;縮圖的淡出淡入也建議改用 CSS transition,把動畫交給合成器執行,比 JS 逐格改 style 順暢得多。另外提醒兩點:一是 mouseover 在行動裝置上沒有意義,正式環境應該改用 click 或 focus 觸發;二是自動播放的輪播務必提供暫停控制項,並尊重 prefers-reduced-motion 設定,這既是無障礙規範的要求,也能避免讓使用者暈眩。
把下面的程式碼存成 html 檔開啟即可預覽(首次載入若未見效果,手動重新整理一次):
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jQuery 幻燈片</title>
<style type="text/css">
body,div,ul,li { margin:0; padding:0;}
#slide {position:relative; width:740px; height:240px; margin:20px auto;}
#pic_list { position:absolute; left:0; bottom:0; width:100%;
list-style: none; overflow:hidden; z-index:2;}
#pic_list li { float:left; width:100px; height:52px; padding-top:10px;}
#pic_list li img { position:absolute; top:18px; width:65px; height:35px;
margin:0 20px; border:1px solid #fff;}
#this_pic { position:absolute; width:100%; height:100%; border:none;}
#transparence { position:absolute; left:0; bottom:0; width:100%; height:50px;
background:#000; z-index:1; border-top:1px solid #fff;}
</style>
<script src="js/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function (){
$('#pic_list img').mouseover(function(){
if($('#this_pic').attr('src') == $(this).attr('src')) return;
$('#this_pic').fadeOut(0).fadeIn(500).attr('src', $(this).attr('src'));
$('#this_a').attr('href', $(this).parents('a').attr('href'));
$('#this_a').attr('title', $(this).parents('a').attr('title'));
$(this).parents('li').nextAll('li').find('img').animate({top:18,opacity:0.6},500);
$(this).parents('li').prevAll('li').find('img').animate({top:18,opacity:0.6},500);
$(this).animate({top:0},500).css('opacity','1');
});
});
$(function(){
$('#transparence').css('opacity','0.4');
$('#pic_list img').css({'opacity':'0.6'});
$('#pic_list img:eq(0)').css({'top':'0','opacity':'1'});
$('#pic_list a').click(function(){return false});
});
</script>
</head>
<body>
<div id="slide">
<a href="#" id="this_a"><img src="images/1.jpg" id="this_pic" /></a>
<ul id="pic_list">
<li><a href="#"><img src="images/1.jpg" alt="" /></a></li>
<li><a href="#"><img src="images/2.jpg" alt="" /></a></li>
<li><a href="#"><img src="images/3.jpg" alt="" /></a></li>
<li><a href="#"><img src="images/4.jpg" alt="" /></a></li>
<li><a href="#"><img src="images/5.jpg" alt="" /></a></li>
</ul>
<span id="transparence"></span>
</div>
</body>
</html>
程式碼不多,效果我覺得還算可以。如果你覺得有哪裡不夠好,或有更漂亮的實作方式,歡迎一起討論。