此代码创建一个无限循环,保留动画并在页面上显示的元素多于页面时在两侧显示导航按钮。功能与_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();
} } ) );
},