該插件實現了功能完整的輪播圖組件,支持:
$("div[data-scro='controler'] b, div[data-scro='controler2'] a").click(function(){
var T = $(this);
if(T.attr("class")=="down") return false; // 當前項不重複觸發
J2ROLLING_ANIMATION.st({
findObject: T, // 觸發元素
main: T.parent().parent().find("div[data-scro='list']"), // 輪播容器
pagSource: T.parent().parent().find("div[data-scro='controler'] b"), // 圓點
className: "down", // 激活樣式
duration: "slow", // 動畫速度
on: $(this)[0].tagName=="A" // 判斷模式:A標簽=無限循環
});
return false;
});
animate: function(T, M, C, S, P, O) {
// 獲取當前激活項索引
var _prevDown = O ? P.parent().find("*[class='"+C+"']") : T.parent().find(T[0].tagName+"[class='"+C+"']");
var _prevIndex = _prevDown.index();
var _thisIndex = O ? (T.attr("class")=="next" ? _prevIndex+1 : _prevIndex-1) : T.index();
var _list = M.find(".item");
// 方向判斷
var p2n = 1; // 1=向左, false=向右
if(T.attr("class")=="prev" || _thisIndex < _prevIndex) p2n = false;
if((T.attr("class")=="next" || _thisIndex > _prevIndex) && T.attr("class")!="prev") p2n = true;
// 無限循環邊界處理
if(O) {
if(_thisIndex==-1) _thisIndex=_list.size()-1;
if(_thisIndex==_list.size()) _thisIndex=0;
P.eq(_thisIndex).addClass(C);
} else {
T.addClass(C);
}
_prevDown.removeClass(C);
// 動畫執行
!p2n ? _list.eq(_thisIndex).css("left", -M.width()) : '';
_list.eq(_prevIndex).animate({left: p2n ? -M.width() : M.width()}, S, function(){
$(this).removeAttr("style");
J2Time = true; // 釋放鎖
});
_list.eq(_thisIndex).animate({left: "0px"}, S);
}
time: function() {
J2SETTIME = window.setInterval(function(){
var num = $("#section-focus-pic div[data-scro='controler'] b[class='down']").index();
var _list = $("#section-focus-pic div[data-scro='list'] li");
_list.eq(num).animate({"left": -$("#section-focus-pic div[data-scro='list']").width()}, "slow", function(){
$(this).removeAttr("style");
$("#section-focus-pic div[data-scro='controler'] b").removeClass("down").eq(num).addClass("down");
});
num++; if(num==_list.size()) num=0;
_list.eq(num).animate({"left": "0px"}, "slow");
}, 4000);
}
| 模式 | 體現位置 | 作用 |
|---|---|---|
| 命名空間 | J2ROLLING_ANIMATION 對象 | 避免全局污染 |
| 狀態鎖 | J2Time 變量 | 防止動畫重入 |
| 配置對象 | st() 參數對象 | 靈活配置行為 |
| 事件委託 | 父容器綁定 click | 動態元素支持 |
更多輪播圖實現方案:jQuery輪播圖多種實現方式總結