首頁 banner 輪播是 jQuery 最常被實作的組件之一。這個版本做仿騰訊 QQ 商城首頁的效果:底部半透明索引條、左右兩側切換箭頭、滑過暫停自動播放,全部用 animate() 完成。
* { margin:0; padding:0; }
body {
font-size:12px; color:#222;
font-family:Verdana,Arial,Helvetica,sans-serif;
background:#f0f0f0;
}
ul, li { list-style:none; }
img { border:0; }
.wrapper { width:800px; margin:0 auto; padding-bottom:50px; }
h1 {
height:50px; line-height:50px; font-size:22px;
font-weight:normal; font-family:"Microsoft YaHei",SimHei;
margin-bottom:20px;
}
/* focus */
#focus { width:800px; height:280px; overflow:hidden; position:relative; }
#focus ul { height:380px; position:absolute; }
#focus ul li {
float:left; width:800px; height:280px;
overflow:hidden; position:relative; background:#000;
}
#focus ul li div { position:absolute; overflow:hidden; }
#focus .btnBg {
position:absolute; width:800px; height:20px;
left:0; bottom:0; background:#000;
}
#focus .btn {
position:absolute; width:780px; height:10px;
padding:5px 10px; right:0; bottom:0; text-align:right;
}
#focus .btn span {
display:inline-block; width:25px; height:10px;
margin-left:5px; cursor:pointer; background:#fff;
}
#focus .btn span.on { background:#fff; }
#focus .preNext {
width:45px; height:100px; position:absolute; top:90px;
background:url(img/sprite.png) no-repeat 0 0; cursor:pointer;
}
#focus .pre { left:0; }
#focus .next { right:0; background-position:right top; }
兩個關鍵設計:#focus 設 overflow:hidden 作為裁切視窗,所有 li 用 float:left 排成一列,靠移動整個 ul 的 left 值來換圖。這就是「左右滑動」而非「淡入淡出」的底層原理。
<div class="wrapper">
<h1>jquery 圖片滾動仿 QQ 商城焦點圖</h1>
<div id="focus">
<ul>
<li><a href="#"><img src="img/01.jpg" alt="焦點圖效果" /></a></li>
<li><a href="#"><img src="img/02.jpg" alt="焦點圖教程" /></a></li>
<li><a href="#"><img src="img/03.jpg" alt="商城焦點圖" /></a></li>
<li><a href="#"><img src="img/04.jpg" alt="焦點圖代碼" /></a></li>
<li><a href="#"><img src="img/05.jpg" alt="焦點圖源碼" /></a></li>
</ul>
</div>
</div>
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">
$(function () {
var sWidth = $("#focus").width(); // 單張圖寬度
var len = $("#focus ul li").length; // 圖片張數
var index = 0;
var picTimer;
// 動態產生底部索引條與左右箭頭
var btn = "<div class='btnBg'></div><div class='btn'>";
for (var i = 0; i < len; i++) {
btn += "<span></span>";
}
btn += "</div><div class='preNext pre'></div>"
+ "<div class='preNext next'></div>";
$("#focus").append(btn);
$("#focus .btnBg").css("opacity", 0.5);
// 滑過小按鈕即跳到對應圖片
$("#focus .btn span")
.css("opacity", 0.4)
.mouseover(function () {
index = $("#focus .btn span").index(this);
showPics(index);
})
.eq(0).trigger("mouseover");
// 左右箭頭的透明度漸變
$("#focus .preNext").css("opacity", 0.2).hover(function () {
$(this).stop(true, false).animate({ "opacity": "0.5" }, 300);
}, function () {
$(this).stop(true, false).animate({ "opacity": "0.2" }, 300);
});
// 上一張
$("#focus .pre").click(function () {
index -= 1;
if (index == -1) index = len - 1;
showPics(index);
});
// 下一張
$("#focus .next").click(function () {
index += 1;
if (index == len) index = 0;
showPics(index);
});
// 所有 li 同一排浮動,所以 ul 寬度要撐成 n 倍
$("#focus ul").css("width", sWidth * len);
// 滑入暫停自動播放,滑出恢復
$("#focus").hover(function () {
clearInterval(picTimer);
}, function () {
picTimer = setInterval(function () {
showPics(index);
index++;
if (index == len) index = 0;
}, 4000); // 4000 毫秒 = 自動播放間隔
}).trigger("mouseleave");
// 依 index 計算 ul 的 left 值並滑動
function showPics(index) {
var nowLeft = -index * sWidth;
$("#focus ul").stop(true, false).animate({ "left": nowLeft }, 300);
$("#focus .btn span")
.stop(true, false).animate({ "opacity": "0.4" }, 300)
.eq(index).stop(true, false).animate({ "opacity": "1" }, 300);
}
});
</script>
效果如下:

1. 定位公式。整個輪播只有一條核心算式:nowLeft = -index * sWidth。第 0 張 left 為 0,第 1 張為 -800px,第 2 張為 -1600px……用負值把 ul 往左推,讓目標圖片進到裁切視窗裡。
2. 索引條動態產生。不寫死在 HTML 裡,而是用迴圈依 li 數量拼字串。圖片增減時,按鈕數量自動跟著變,不必改兩處。
3. stop(true, false) 的必要性。自動播放每 4 秒觸發一次,動畫 300ms 就結束,正常情況不會堆疊;但使用者快速連點箭頭時會。加上 stop 可以避免動畫排隊造成的「滑過頭」。
4. 循環邊界處理。點上一張到第一張時 index 變 -1,要回繞成 len - 1;點下一張過了最後一張則回 0。這是最基本但最容易漏寫的邏輯。
用 .on() 取代已廢棄的 .live()/舊式 bind()。範例中的 mouseover、hover 在新版 jQuery 仍可執行,但建議統一寫成 .on("mouseenter", handler)。
索引按鈕改用 click 而非 mouseover。滑過就切換看似炫,但在觸控裝置上完全無效,也容易誤觸。可保留 mouseover 但同時綁 click。
用 CSS transition 取代 jQuery animate。位移動畫交給 GPU 處理會順很多:
#focus ul {
transition: left .3s ease;
will-change: left;
}
然後腳本裡只改 $("#focus ul").css("left", nowLeft) 即可,動畫交給瀏覽器。
圖片要給寬高。所有 <img> 都應標上 width/height(或用 CSS aspect-ratio),否則圖片載入時 sWidth 的計算時機會出問題,首張圖可能位置錯亂。
記得 clearInterval 的時機。頁面若被放入 iframe 或單頁應用程式的路由切換中,計時器不會自動消失。在 $(window).on('unload') 或路由離開時清掉,可避免記憶體與錯誤。