malsup / cycle2

2nd gen cycling
899 stars 236 forks source link

Fixed a bug in the remove command #687

Open ghost opened 9 years ago

ghost commented 9 years ago

The remove command updates the currSlide but not the nextSlide. As a result a slide will be skipped in the slideshow. This snippet fixes the bug:

   remove: function( index ) {
        var opts = this.opts();
        var slide, slideToRemove, slides = [], slideNum = 1;
        for ( var i=0; i < opts.slides.length; i++ ) {
            slide = opts.slides[i];
            if ( i == index ) {
                slideToRemove = slide;
            }
            else {
                slides.push( slide );
                $( slide ).data('cycle.opts').slideNum = slideNum;
                slideNum++;
            }
        }
        if ( slideToRemove ) {
            opts.slides = $( slides );
            opts.slideCount--;
            $( slideToRemove ).remove();
            if (index == opts.currSlide) {
                opts.currSlide = (opts.currSlide + opts.slideCount - 1) % opts.slideCount;
                opts.nextSlide = Math.max(opts.nextSlide - 1, 0);
                opts.API.advanceSlide( 1 );
            }
            else if ( index < opts.currSlide ) {
                opts.currSlide--;
                opts.nextSlide = Math.max(opts.nextSlide - 1, 0);
            }
            else {
                opts.nextSlide = opts.nextSlide % opts.slideCount;
            }

            opts.API.trigger('cycle-slide-removed', [ opts, index, slideToRemove ]).log('cycle-slide-removed');
            opts.API.updateView();
        }
    }