0

我使用 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 来获得一种效果?或者我可以将它用于不止一种效果,但我只是不知道如何使用它?

4

1 回答 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>
于 2020-05-05T09:54:39.737 回答