当循环为真时,iDangero.us滑动滑块计数

Hak*_*aya 3 javascript jquery slider swiper

我将iDangero.us Swiper js用于网页,初始化代码如下:

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

我需要获取当前的滑块索引和滑块总数。Swiper API提供了mySwiper.activeIndex属性和mySwiper.slides,但是问题是,当循环为true时,它们不会提供正确的索引和计数。

循环为真时,是否有任何方法可以正确获取这些数字?

Hen*_*k N 6

activeIndex当涉及循环时,幻灯片的数量,因此有时是“错误”的设计:https : //github.com/nolimits4web/Swiper/issues/1205

我能找到的获得幻灯片总数的最佳方法是:

mySwiper.slides.length - 2
Run Code Online (Sandbox Code Playgroud)

您可以使用它来获取当前索引(这是从零开始的):

(mySwiper.activeIndex - 1) % (mySwiper.slides.length - 2)
Run Code Online (Sandbox Code Playgroud)

当然,这并不理想。您可以打开一个 GitHub 问题并建议添加更方便的方法来访问这些值。

  • 现在已经添加了 realIndex 属性!https://github.com/nolimits4web/Swiper/pull/1697 (3认同)

Pet*_*dez 5

截至2016年5月,他们已经添加了realIndex属性!

需要注意的事情:1.)realIndex属性以字符串而不是整数形式返回(以防万一,您需要对它进行数学运算)2.)realIndex属性以0(应该如此)开头,这与activeIndex不同在循环模式下,我的情况是从1开始

https://github.com/nolimits4web/Swiper/pull/1697