【问题标题】:How to use " swiper.activeIndex " with if condition?如何在 if 条件下使用“swiper.activeIndex”?
【发布时间】:2019-07-24 04:59:51
【问题描述】:

我在我的项目中使用 Swiper.js。我想知道当前活动的滑块。

如何将swiper.activeIndexif 条件一起使用?

或者,我想为到达预期滑块时编写一些代码。

【问题讨论】:

    标签: javascript jquery html swiper


    【解决方案1】:

    您好,您需要做的是监听您的 swiper 的“slideChange”事件。

    要了解有关 swiper 的事件和 api 的更多信息,请查看文档:https://idangero.us/swiper/api/

    这是一个关于如何做到这一点的简短示例:

    <script>
    var swiper = new Swiper('.swiper-container');
        swiper.on('slideChange', function () {
            if(this.activeIndex === 1) {
                console.log("IM ON SECOND SLIDE!");
                alert("IM ON SECOND SLIDE!");
            }
        });
    
    </script>
    

    完整工作示例(代码取自演示页面):

    <!DOCTYPE html>
    <html lang="en">
    <head>
      <meta charset="utf-8">
      <title>Swiper demo</title>
      <!-- Link Swiper's CSS -->
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.5.0/css/swiper.min.css">
      <script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.5.0/js/swiper.min.js"></script>
      <!-- Demo styles -->
      <style>
        html, body {
          position: relative;
          height: 100%;
        }
        body {
          background: #eee;
          font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
          font-size: 14px;
          color:#000;
          margin: 0;
          padding: 0;
        }
        .swiper-container {
          width: 100%;
          height: 100%;
        }
        .swiper-slide {
          text-align: center;
          font-size: 18px;
          background: #fff;
          /* Center slide text vertically */
          display: -webkit-box;
          display: -ms-flexbox;
          display: -webkit-flex;
          display: flex;
          -webkit-box-pack: center;
          -ms-flex-pack: center;
          -webkit-justify-content: center;
          justify-content: center;
          -webkit-box-align: center;
          -ms-flex-align: center;
          -webkit-align-items: center;
          align-items: center;
        }
      </style>
    </head>
    <body>
      <!-- Swiper -->
      <div class="swiper-container">
        <div class="swiper-wrapper">
          <div class="swiper-slide">Slide 1</div>
          <div class="swiper-slide">Slide 2</div>
          <div class="swiper-slide">Slide 3</div>
          <div class="swiper-slide">Slide 4</div>
          <div class="swiper-slide">Slide 5</div>
          <div class="swiper-slide">Slide 6</div>
          <div class="swiper-slide">Slide 7</div>
          <div class="swiper-slide">Slide 8</div>
          <div class="swiper-slide">Slide 9</div>
          <div class="swiper-slide">Slide 10</div>
        </div>
      </div>
    
      <!-- Initialize Swiper -->
      <script>
        var swiper = new Swiper('.swiper-container');
        swiper.on('slideChange', function () {
          if(this.activeIndex === 1) {
            console.log("IM ON SECOND SLIDE!");
            alert("IM ON SECOND SLIDE!");
          }
        });
    		
      </script>
    </body>
    </html>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多