How to modify Elastislide so it loops infinitely

后端 未结 7 1139
庸人自扰
庸人自扰 2021-01-21 08:42

I\'ve been searching for an image carousel that will display several images at once, is responsive and loops infinitely.

Elastislide seems to be the most suitable ( http

7条回答
  •  [愿得一人]
    2021-01-21 09:06

    This code creates an infinite loop, preserves animations and shows navigation buttons on both sides if there are more elements than displayed on page. The _toggleControls function remains the same as in the original version.

    // 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 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();
            } } ) );
    
        },
    

提交回复
热议问题