輪播圖幾乎是每個首頁都會用到的元件。趁著手邊不忙,自己從零練習實作了一次,功能與視覺都跑起來了,但仍有不少地方可以再打磨——這篇算是記錄一下自己研究 JavaScript 的過程,也方便日後回頭對照。
把 jQuery 的連結與圖片路徑換成自己的資源,就能直接跑起來。
整個輪播由三塊組成:一個固定尺寸的容器、一條橫向排列的 ul 圖片帶,以及下方的頁碼指示器與左右箭頭。
<div id="pic_carousel">
<ul id="lunbo_pic" class="clearfix lunbo_pic">
<li><a href="#"><img src="images/insco_p1.jpg" /></a></li>
<li><a href="#"><img src="images/insco_p2.jpg" /></a></li>
<li><a href="#"><img src="images/insco_p3.jpg" /></a></li>
</ul>
<div id="lunbo_curso" class="lunbo_curso">
<a href="#" class="small_xz">1</a>
<a href="#">2</a>
<a href="#">3</a>
</div>
<span id="arr_l" class="arr"></span>
<span id="arr_r" class="arr"></span>
</div>
#pic_carousel { position: relative; width: 1000px; height: 350px; overflow: hidden; margin: 0 auto; }
.lunbo_pic { position: absolute; left: 0; top: 0; }
.lunbo_pic li { float: left; }
.lunbo_curso { position: absolute; left: 50%; bottom: 0; }
.lunbo_curso .small_xz { color: #F0F; } /* 目前頁碼的高亮樣式 */
.arr { position: absolute; top: 50%; margin-top: -25px; width: 30px; height: 50px; }
#arr_l { left: 0; }
#arr_r { right: 0; }
容器 overflow: hidden 是輪播的靈魂,超出部分被裁掉,只剩下目前這一屏。
var b_width = 1000; // 單張圖寬度
var speed = 500; // 切換動畫速度
var s_time = 3000; // 自動輪播間隔
var pic_li = $("#lunbo_pic").children("li");
$(document).ready(function () {
$("#lunbo_pic").width(pic_li.width() * pic_li.length); // 圖片帶總寬
var small_width = $(".lunbo_curso > a").width() * $(".lunbo_curso > a").length;
$(".lunbo_curso").width(small_width);
$(".lunbo_curso").css("margin-left", -small_width / 2); // 頁碼置中
});
function prePage() {
if ($(".small_xz").index() == 0) {
// 已在第一張,直接跳到最後一張
$("#lunbo_pic").css("left", -4000);
$("#lunbo_pic").animate({ "left": -parseInt(pic_li.length * b_width - b_width) }, speed);
$("#lunbo_curso > a").eq(pic_li.length - 1).addClass("small_xz").siblings().removeClass("small_xz");
} else {
$("#lunbo_curso > a").eq($(".small_xz").index() - 1).addClass("small_xz").siblings().removeClass("small_xz");
$("#lunbo_pic").animate({ "left": parseInt($("#lunbo_pic").css("left")) + b_width }, speed);
}
}
function nextPage() {
if ($(".small_xz").index() == pic_li.length - 1) {
// 已在最後一張,回到第一張
$("#lunbo_pic").css("left", 0);
$("#lunbo_curso > a").eq(0).addClass("small_xz").siblings().removeClass("small_xz");
} else {
$("#lunbo_curso > a").eq($(".small_xz").index() + 1).addClass("small_xz").siblings().removeClass("small_xz");
$("#lunbo_pic").animate({ "left": parseInt($("#lunbo_pic").css("left")) - b_width }, speed);
}
}
$(document).on("click", function (e) {
var $target = $(e.target);
// 點頁碼:跳到對應位置
if ($target.is("a") && $target.parent().attr("id") == "lunbo_curso") {
$target.addClass("small_xz").siblings().removeClass('small_xz');
$("#lunbo_pic").animate({ left: -parseInt($target.index() * b_width) }, speed);
}
if ($target.attr('id') == "arr_l") prePage();
if ($target.attr('id') == "arr_r") nextPage();
});
intervalTime = setInterval(nextPage, s_time);
$("#pic_carousel").on("mouseover", function () { clearInterval(intervalTime); }); // 滑入暫停
$("#pic_carousel").on("mouseout", function () { intervalTime = setInterval(nextPage, s_time); }); // 移出恢復
$(document).live() 已在 jQuery 1.9 中移除,上面改用 on() 並直接綁在容器上,效能與相容性都更好。.small_xz 的 index 更直觀,也能避免頁碼樣式被別處改動時出錯。b_width。很多事看著簡單,動手才發現細節一堆。這是自己前端路上的一小步,相信之後能寫出更好的版本。