側欄那種一張一張換的圖片小輪播,看起來簡單,其實是由三層容器分工完成的:外層負責裁切、內層負責換圖與淡入、下層負責數字導覽。把這三層拆開來看,維護與改樣式都會輕名校。
<style>
#f_div { width:150px; height:100px; margin:0 auto; overflow:hidden; }
#f_imgDiv { width:100%; height:100px; overflow:hidden; }
#f_img {
filter: progid:DXImageTransform.Microsoft.Fade(Overlap=1.00);
border:0;
}
#f_infoDiv { width:100%; top:-20px !important; top:-22px; position:relative; }
#f_buttonDiv { width:100%; height:21px; overflow:hidden; text-align:left; }
#f_buttonDiv div { width:1px; height:21px; background:#fff; float:right; }
#f_buttonDiv .bg { width:21px; height:21px; background:#fff; float:right;
filter:Alpha(Opacity=40); }
#f_buttonDiv a {
width:21px; height:17px; float:right; padding-top:5px;
color:#fff; font-family:Arial; font-size:10px;
text-align:center; display:block; overflow:hidden;
text-decoration:none; position:absolute;
}
#f_buttonDiv a:link,
#f_buttonDiv a:visited,
#f_buttonDiv a:active { color:#000; }
#f_buttonDiv a:hover { background:#FF840C; color:#fff; }
#f_buttonDiv a.on:link,
#f_buttonDiv a.on:visited,
#f_buttonDiv a.on:hover { background:#FF840C; color:#fff; }
#f_text { height:20px; line-height:20px; overflow:hidden; text-align:center; }
#f_text a:hover { color:#FF6600; }
</style>
<div id="ad_pic">
<div id="f_div">
<div id="f_imgDiv"></div> <!-- 圖片區 -->
<div id="f_infoDiv">
<div id="f_buttonDiv"></div> <!-- 數字按鈕區 -->
</div>
</div>
<div id="f_text"></div> <!-- 焦點文字區 -->
</div>
這份素材裡附了一個很實用的小函式,用同一組 id 序號批次切換「按鈕高亮」與「內容顯示」,可謂一函多用:
function $(id) { return document.getElementById(id); }
function change_menu(id, content_id, num, total, menu_on, menu_off) {
for (var i = 1; i <= total; i++) {
$(id + i).className = menu_off;
$(content_id + i).style.display = 'none';
}
$(id + num).className = menu_on;
$(content_id + num).style.display = 'block';
}
var imgWidth = 150;
var imgHeight = 100;
var _timeOut_ = 5000; // 每張圖停留時間
var show_text = false; // 是否顯示焦點文字
var timeOut = _timeOut_;
var timeOut2 = _timeOut_ / 2; // 滑鼠移出後重新啟動的等待
top:-20px !important; top:-22px; 是為了避開特定瀏覽器的解讀差異,今天已無必要。filter:Alpha(Opacity=40) 讓按鈕背後有一層淡色,白字才看得清楚。.current 是同一個概念,只是換了名字。要現代化的話,Fade 與 Alpha 這兩個 filter 都只在舊 IE 有效,改用 opacity 與 transition 就能跨瀏覽器;而 float: right 排出的數字列,用 flexbox 會更穩定,順序也不會因為 float 而顛倒。