当我添加一个以上的swiper元素时的奇怪行为。

问题描述 投票:0回答:1

我使用swiper.js框架,当我添加一个效果元素时,一切都正常。

`<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Swiper demo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, minimum-scale=1, maximum-scale=1">

  <!-- Link Swiper's CSS -->
  <link rel="stylesheet" href="../package/css/swiper.min.css">

  <!-- 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>
    <!-- Add Pagination -->
    <div class="swiper-pagination"></div>
  </div>

  <!-- Swiper JS -->
  <script src="../package/js/swiper.min.js"></script>

  <!-- Initialize Swiper -->
  <script>
    var swiper = new Swiper('.swiper-container', {
      pagination: {
        el: '.swiper-pagination',
        dynamicBullets: true,
      },
    });
  </script>
</body>
</html>

但当我添加另一个swiper元素时,它的行为与第一个元素相同,我的意思是它们的行为是一样的,但它们应该是不同的,因为它们是... ...

    `<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" href="https://unpkg.com/swiper/css/swiper.min.css">
    <script src="https://kit.fontawesome.com/b8a898bb4c.js" crossorigin="anonymous"></script>
    <link rel="stylesheet" href="./css/style.css">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>..</title>
</head>
<body>
    <div class="swiper-container">
        <div class="swiper-wrapper">
            <div class="swiper-slide menu">Menu slide</div>
            <div class="swiper-slide content">
                <div class="menu-button">
                    <div class="bar"></div>
                    <div class="bar"></div>
                    <div class="bar"></div>
                </div>
                Content slide
            </div>
        </div>
    </div>

    <section id="best-selling">
        <div class="swiper-container" style="cursor: unset !important;">
            <div class="swiper-wrapper">
            <div class="swiper-slide slide-cst">
                <div> 
                  <img src="" alt="">
                      <div>
                          <h5></h5>
                          <p></p>
                      </div>
                  </div>
            </div>
            <div class="swiper-slide slide-cst">
                <div> 
                  <img src="" alt="">
                      <div>
                          <h5></h5>
                          <p></p>
                      </div>
                  </div>
            </div>
            <div class="swiper-slide slide-cst">
                <div> 
                  <img src="" alt="">
                      <div>
                          <h5></h5>
                          <p></p>
                      </div>
                  </div>
              </div>
              <div class="swiper-slide slide-cst">
                  <div> 
                    <img src="" alt="">
                        <div>
                            <h5></h5>
                            <p></p>
                        </div>
                    </div>
                </div>
            </div>
    </section>
    <script src="https://unpkg.com/swiper/js/swiper.min.js"></script>
  <script>
    var swiper = new Swiper('.swiper-container', {
      effect: 'coverflow',
      grabCursor: false,
      centeredSlides: true,
      slidesPerView: 'auto',
      coverflowEffect: {
        rotate: 50,
        stretch: 0,
        depth: 100,
        modifier: 1,
        slideShadows : true,
      },
      pagination: {
        el: '.swiper-pagination',
      },
    });
</script>
    <script>

    var menuButton = document.querySelector('.menu-button');
    var openMenu = function () {
      swiper.slidePrev();
    };
    var swiper = new Swiper('.swiper-container', {
      slidesPerView: 'auto',
      initialSlide: 1,
      resistanceRatio: 0,
      slideToClickedSlide: true,
      on: {
        slideChangeTransitionStart: function () {
          var slider = this;
          if (slider.activeIndex === 0) {
            menuButton.classList.add('cross');
            // required because of slideToClickedSlide
            menuButton.removeEventListener('click', openMenu, true);
          } else {
            menuButton.classList.remove('cross');
          }
        }
        , slideChangeTransitionEnd: function () {
          var slider = this;
          if (slider.activeIndex === 1) {
            menuButton.addEventListener('click', openMenu, true);
          }
        },
      }
    });

  </script>
</body>
</html>`

我试图改变类名,但它没有,我必须只用swiper来实现一个效果吗? 或者我可以用它来实现多个效果,但我只是不知道如何使用它?

swiper swiperjs
1个回答
0
投票

我相信你需要初始化两个新的Swiper方法,用不同的类。

因此,类似的东西。

var swiperOne = new Swiper('.swiper-one', {})...
var swiperTwo = new Swiper('.swiper-two', {})...

和这样的标记:

<div class="swiper-container swiper-one">...</div>
and 
<div class="swiper-container swiper-two">...</div>
© www.soinside.com 2019 - 2024. All rights reserved.