Javascript 如何检测swiper js中的当前幻灯片?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32945099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 14:24:23  来源:igfitidea点击:

How to detect current slide in swiper js?

javascriptswiper

提问by Ravi Ranjan

Working with swiper js for a slider and want to detect the current image/slide. how i can detect with HTML and JS? any idea?

为滑块使用 swiper js 并希望检测当前图像/幻灯片。我如何使用 HTML 和 JS 进行检测?任何的想法?

<div class="swiper-container">
    <div class="swiper-wrapper" align="center">
        <div class="swiper-slide">
            <img src="images/card_gold.png" width="80%" align="middle" onclick="desc(\'' + card1 + '\')">
        </div>
        <div class="swiper-slide">
            <img src="images/card_platinum.png" width="80%" align="middle" onclick="desc(\'' + card2 + '\')">
        </div>
        <div class="swiper-slide">
            <img src="images/card_silver.png" width="80%" align="middle" onclick="desc(\'' + card3 + '\')">
        </div>
    </div>
    <!-- Add Arrows -->
    <div class="swiper-button-next"></div>
    <div class="swiper-button-prev"></div>
</div>

回答by Timsta

its very easy. just use this:

它很容易。只需使用这个:

swiper.activeIndex

回答by Michel Carroll

As far as I can see from their demos, the current slide always has the .swiper-slide-activeclass on the current slide element.

据我从他们的演示中看到,当前幻灯片始终具有.swiper-slide-active当前幻灯片元素上的类。

You can use jQuery selectors to get properties from the active slide. Here's an example of me fetching its image source:

您可以使用 jQuery 选择器从活动幻灯片中获取属性。这是我获取其图像源的示例:

$('.swiper-slide-active img').attr('src')

回答by Torben

As of May 2016 they have added the realIndex property!

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

swiper.realIndex

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

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

Notice:

注意:

  • property is returned as a string
  • property starts with 0, unlike activeIndex in loop mode which in my case started with 1
  • 属性作为字符串返回
  • 属性以 0 开头,与循环模式中的 activeIndex 不同,在我的情况下,它以 1 开头