如何在react组件中创建多个swiper实例并通过另一个组件的滑动来控制一个实例?

时间:2019-09-06 20:58:09

标签: reactjs swiper

我有一个react组件,需要连接多个转盘。我正在尝试在react组件中创建两个swiper实例,并在另一个的幻灯片上控制一个的幻灯片。 我需要帮助来创建多个刷卡实例并在React中使用。 并控制另一个的移动。

1 个答案:

答案 0 :(得分:0)

常规-通过控制器

否则,您可以使用控制器(那里没有很多文档/示例-对于特定的答案,您应该添加完整的代码示例):

swiper API: https://swiperjs.com/api/#controller

Github: Github issue: How do I get two swiper instances to control each other? #1738

示例

let sliderOne = new Swiper(".slider-one", {
  pagination: {
    el: ".swiper-pagination",
    clickable: true
  }
});
let sliderTwo = new Swiper(".slider-two", {
  pagination: {
    el: ".swiper-pagination",
    clickable: true
  }
});

sliderOne.controller.control = sliderTwo;
sliderTwo.controller.control = sliderOne;
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: 50%;
}
.swiper-slide {
  text-align: center;
  font-size: 18px;
  /* 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;
}
.swiper-pagination-bullet {
  width: 20px;
  height: 20px;
  text-align: center;
  line-height: 20px;
  font-size: 12px;
  color: #000;
  opacity: 1;
  background: rgba(0, 0, 0, 0.2);
}
.swiper-pagination-bullet-active {
  color: #fff;
  background: #007aff;
}


.slider-two .swiper-pagination-bullet-active{
   background: red;
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.5.1/css/swiper.min.css" rel="stylesheet"/>

<div class="swiper-container slider-one">
    <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>
<div class="swiper-container slider-two">
    <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>
  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/Swiper/4.5.1/js/swiper.min.js"></script>

拇指

两个轮播点击/滑动A更改B-非常容易用拇指控制/同步:

  

除了Controller组件之外,Swiper还附带Thumbs组件   旨在与更多的拇指扫动一起工作   比用于同步两个刷卡器的控制器更正确。

https://swiperjs.com/api/#thumbs

演示:

演示源代码: