此代碼創建無限循環,保留動畫並在兩側顯示導航按鈕(如果頁面上顯示的元素多於此元素)。 _toggleControls
功能與原始版本保持一致。
// modified version of _slide
_slide : function(dir, val, anim, callback) {
// if animating return
if(this.$slider.is(':animated'))
return false;
// current margin left
var ml = parseFloat(this.$slider.css('margin-left'));
// val is just passed when we want an exact value for the margin left (used in the _slideToCurrent function)
if(val === undefined) {
// how much to slide?
var amount = this.fitCount * this.itemW, val;
if(amount < 0) return false;
// make sure not to leave a space between the last item/first item and the end/beggining of the slider available width
if(dir === 'right' && this.sliderW - (Math.abs(ml) + amount) < this.visibleWidth) {
for (var i=0;i<this.fitCount;i++) { // add elements
this.$slider.css('margin-left', '+=' + this.itemW);
this.$slider.append(this.$slider.children('li:first').clone()) ;
this.$slider.children('li:first').remove();
}
} else if (dir === 'left' && Math.abs(ml) - amount < 0) {
for (var i=0;i<this.fitCount;i++) { // add elements
this.$slider.css('margin-left', '-=' + this.itemW);
this.$slider.prepend(this.$slider.children('li:last').clone()) ;
this.$slider.children('li:last').remove();
}
}
(dir === 'right') ? val = '-=' + amount : val = '+=' + amount
}
else {
var fml = Math.abs(val); // future margin left
if(Math.max(this.sliderW, this.visibleWidth) - fml < this.visibleWidth) {
val = - (Math.max(this.sliderW, this.visibleWidth) - this.visibleWidth);
if(val !== 0)
val += this.options.margin; // decrease the margin left if not on the first position
// show/hide navigation buttons
this._toggleControls('right', -1);
fml = Math.abs(val);
}
// show/hide navigation buttons
if(this.fitCount < this.itemsCount)
this._toggleControls('left', 1);
else
this._toggleControls('left', -1);
if(Math.max(this.sliderW, this.visibleWidth) - this.visibleWidth > fml + this.options.margin)
this._toggleControls('right', 1);
else
this._toggleControls('right', -1);
}
$.fn.applyStyle = (anim === undefined) ? $.fn.animate : $.fn.css;
var sliderCSS = { marginLeft : val };
var instance = this;
this.$slider.applyStyle(sliderCSS, $.extend(true, [], { duration : this.options.speed, easing : this.options.easing, complete : function() {
if(callback) callback.call();
} }));
},
很不錯的答案,你有我的投票,但還有一件事,如何防止轉盤滑回第一項幾秒鐘,點擊導航控件後? – motto