我在网页上使用了iDangero.us Swiper js,初始化代码如下所示:
var mySwiper = new Swiper( '.swiper-container', {
direction: 'horizontal',
loop: true,
speed: 600,
nextButton: '.slider-control-next',
prevButton: '.slider-control-prev',
} );我需要得到当前滑块指数和滑块总数。Swiper提供了mySwiper.activeIndex属性和mySwiper.slides,但问题是当循环为true时,它们没有给出正确的索引和计数。
当循环为真时,有任何方法可以正确地得到这些数字吗?
发布于 2015-03-31 14:51:56
当涉及循环时,幻灯片的数量,因而有时也是activeIndex,在设计上是“错误的”:https://github.com/nolimits4web/Swiper/issues/1205。
我能找到的获取幻灯片总数的最好方法是:
mySwiper.slides.length - 2您可以使用它来获取当前索引(这个索引是基于零的):
(mySwiper.activeIndex - 1) % (mySwiper.slides.length - 2)当然,这并不理想。您可以使用打开一个GitHub问题并建议添加更方便的方法来访问这些值。
发布于 2017-01-10 02:27:43
截至2016年5月,他们增加了realIndex属性!
需要注意的事情: 1.)realIndex属性作为字符串而不是整数返回(以防您需要对其进行数学计算)2)。realIndex属性以0开头(正如它应该的那样),与循环模式中的activeIndex不同,在我的例子中,循环模式以1开头
发布于 2016-07-11 09:58:22
只是添加了另一个答案,因为Swiper还没有包含realIndex属性。这里有一个很好的方法来获得真正的索引时循环,而不减去硬编码的数字(这可能很容易改变)。
var realIndex = e.slides.eq(e.activeIndex).attr('data-swiper-slide-index');像这样使用:
var slider = new Swiper('.swiper-container');
slider.on('slideChangeEnd', function(e) {
var realIndex = e.slides.eq(e.activeIndex).attr('data-swiper-slide-index');
// do whatever
});https://stackoverflow.com/questions/29348926
复制相似问题