Hakim Jaya Hakim Jaya - 7 months ago 102
Javascript Question

iDangero.us Swiper slide count when loop is true

I'm using iDangero.us Swiper js for a webpage, and initialization code is as following:

var mySwiper = new Swiper( '.swiper-container', {
direction: 'horizontal',
loop: true,
speed: 600,
nextButton: '.slider-control-next',
prevButton: '.slider-control-prev',
} );


And I need to get current slider index and total count of sliders. Swiper API provides mySwiper.activeIndex property and mySwiper.slides but the problem is that when loop is true they don't give correct index and count.

Is there any way to get these numbers correctly when loop is true?

Answer

The number of slides, and thus sometimes the activeIndex, is "wrong" by design when loops are involved: https://github.com/nolimits4web/Swiper/issues/1205

Best way I could find to get the total number of slides is:

mySwiper.slides.length - 2

You could use that to get the current index (this one is zero-based):

(mySwiper.activeIndex - 1) % (mySwiper.slides.length - 2)

This is not ideal, of course. You could open a GitHub issue and propose adding more convenient ways of accessing these values.

Comments