DIV CSS 佈局教程網

基於 jQuery 的圖片廣告輪播效果
編輯:JQuery特效代碼     

效果圖:

JavaScript 程式碼:

var curr = 0, next = 0, count = 0;
$(document).ready(function () {
// 記錄圖片數量
count = $('#img_list a').size();
t = setInterval('imgPlay()', 3000);
// 滑鼠移到圖片或導航上停止播放,移開後恢復
$('#imgs li, #img_list a').hover(function () {
clearInterval(t);
}, function () {
t = setInterval('imgPlay()', 3000);
});
// 點擊導航跳到對應圖片
$('#img_list a').click(function () {
var index = $('#img_list a').index(this);
if (curr != index) {
play(index);
curr = index;
}
return false;
});
});
// 播放函數
var imgPlay = function () {
next = curr + 1;
if (curr == count - 1) next = 0;
play(next);
curr++;
if (curr > count - 1) { curr = 0; next = curr + 1; }
};
// 控制播放效果
var play = function (next) {
$('#imgs li').eq(curr).css({ 'opacity': '0.5' }).animate({ 'left': '-500px', 'opacity': '1' }, 'slow', function () {
$(this).css({ 'left': '490px' });
}).end()
.eq(next).animate({ 'left': '0px', 'opacity': '1' }, 'slow', function () {
$('#img_list a').siblings('a').removeClass('active').end().eq(next).addClass('active');
});
};

結構上,每張圖片為一個 <li>,透過 animate 動畫讓當前圖向左滑出、下一張滑入,並同步切換底部導航的高亮狀態。搭配 setInterval 定時自動播放,以及 hover 暫停、點擊導航跳轉等互動設計,即可做出流暢的圖片輪播廣告。完整 HTML 與 CSS 請參見原文。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved