简体   繁体   English

如何在单个页面上显示多个 Swiper 幻灯片?

[英]How can I have multiple Swiper slideshows on a single page?

I'm using Swiper Slideshow .我正在使用Swiper Slideshow I'm using this particular version .我正在使用这个特定版本 Here's the exact code I'm using.这是我正在使用的确切代码

When adding a second Swiper, the pagination doesn't work properly.添加第二个 Swiper 时,分页无法正常工作。

I tried giving a different class to the second Swiper container but it didn't work.我试着给第二个 Swiper 容器一个不同的 class 但它没有用。

How can I have two of this in the same page?我怎样才能在同一页上有两个呢?

Thanks.谢谢。

Their support sent me this DEMO .他们的支持给我发了这个DEMO It works!有用!

You don't need to do anything to the JS File.你不需要对 JS 文件做任何事情。 All you need is to add an extra class to pagination, add an extra class also to the slideshow, and differentiate the rest of the classes on everything else (see code below).您所需要的只是为分页添加一个额外的类,向幻灯片添加一个额外的类,并在其他所有内容上区分其余类(参见下面的代码)。 With this you can have as many slishows as you want in the same page.有了这个,您可以在同一页面中拥有任意数量的幻灯片。

<div class="swiper-container swiper1">
        <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 swiper-pagination1"></div>
    </div>

    <!-- Swiper -->
    <div class="swiper-container swiper2">
        <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 swiper-pagination2"></div>

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

<!-- Initialize Swiper -->
<script>
var swiper1 = new Swiper('.swiper1', {
    pagination: '.swiper-pagination1',
    paginationClickable: true,
});
var swiper2 = new Swiper('.swiper2', {
    pagination: '.swiper-pagination2',
    paginationClickable: true,
});
<script>

In case someone is passing here in search for a solution to conflicts with prev and next button between multiple swipers, this is what fixed it for me (Nuxt.js/SSR project):如果有人经过这里寻找解决多个滑动器之间的上一个和下一个按钮冲突的解决方案,这就是为我解决的问题(Nuxt.js/SSR 项目):

1) Add id attributes to button's divs: <div id="button-next-relacionados" class="swiper-button-next swiper-button-white"></div> <div id="button-next-relacionados" class="swiper-button-next swiper-button-white"></div> 1) 为按钮的 div 添加 id 属性: <div id="button-next-relacionados" class="swiper-button-next swiper-button-white"></div> <div id="button-next-relacionados" class="swiper-button-next swiper-button-white"></div>

2) In the swiper options object, refer to the new ids instead of swiper-button-prev and swiper-button-next classes: 2) 在 swiper 选项对象中,引用新的 ids 而不是swiper-button-prevswiper-button-next类:

swiperOption: {
        direction: 'horizontal',
        slidesPerView: 4,
        spaceBetween: 6,
        navigation: {
          nextEl: '#button-next-relacionados',
          prevEl: '#button-prev-relacionados'
        },

I had a case where I needed 3 identical sliders on the same page and I didn't want to duplicate any code, nor did I want to hardcode anything.我有一个案例,我在同一页面上需要 3 个相同的滑块,我不想复制任何代码,也不想硬编码任何东西。 This is how I solved it 👇我就是这样解决的👇

// Function that actually builds the swiper 
const buildSwiperSlider = sliderElm => {
    const sliderIdentifier = sliderElm.dataset.id;
    return new Swiper(`#${sliderElm.id}`, {
        navigation: {
            nextEl: `.swiper-button-next-${sliderIdentifier}`,
            prevEl: `.swiper-button-prev-${sliderIdentifier}`
        },
        pagination: {
            el: `.swiper-pagination-${sliderIdentifier}`,
            type: 'progressbar',
        },
    });
}

// Get all of the swipers on the page
const allSliders = document.querySelectorAll('.swiper');

// Loop over all of the fetched sliders and apply Swiper on each one.
allSliders.forEach(slider => buildSwiperSlider(slider));

As you can see from the above code the key here is assigning an identifier to each of the sliders.从上面的代码可以看出,这里的关键是为每个滑块分配一个标识符。 In my case, I opted to use a data-id property and reference that.就我而言,我选择使用data-id属性并引用它。 Like this 👇像这样👇

<div class="swiper" data-id="ID_OF_FIRST_SLIDER">
...
</div>
<div class="swiper" data-id="ID_OF_SECOND_SLIDER">
...
</div>
<div class="swiper" data-id="ID_OF_THIRD_SLIDER">
...
</div>

And pagination/navigation element just incorporate the identifier as part of their class name.分页/导航元素只是将标识符作为其类名的一部分。

My current Swiper version is 3.4.2.我当前的 Swiper 版本是 3.4.2。 When I click on the next-prev button, all the sliders on the page moves.当我单击 next-prev 按钮时,页面上的所有滑块都会移动。

For set different next-prev buttons I done this:为了设置不同的 next-prev 按钮,我这样做了:

<div class="swiper-pager">
  <div class="swiper-button-next></div>
  <div class="swiper-button-prev></div>
</div>

=> =>

<div class="swiper-pager">
  <div class="swiper-button-next swiper-button-next_slideshow<?=$module?>"></div>
  <div class="swiper-button-prev swiper-button-prev_slideshow<?=$module?>"></div>
</div>

And in js:在js中:

$('#slideshow<?=$module?>').swiper({
  mode: 'horizontal',
  slidesPerView: 1,
  pagination: '.slideshow<?=$module?>',
  paginationClickable: true,
  // nextButton: '.swiper-button-next', // not work properly
  // prevButton: '.swiper-button-prev', // not work properly
  nextButton: '.swiper-button-next_slideshow<?=$module?>',
  prevButton: '.swiper-button-prev_slideshow<?=$module?>',
  spaceBetween: 30,
  autoplay: 2500,
  autoplayDisableOnInteraction: true,
  loop: true
});

@David Martins Thanks a lot, but in current version 4.5.0, you need to modify a little js code: @David Martins 非常感谢,但在当前版本 4.5.0 中,您需要修改一些 js 代码:

<script type="text/javascript">
// Init Swiper
var swiper1 = new Swiper('.swiper1', {
  pagination: {
    el: '.swiper-pagination1',
    clickable: true,
  },
  navigation: {
    nextEl: '.swiper-button-next',
    prevEl: '.swiper-button-prev',
  },
  // Enable debugger
  debugger: true,
});
var swiper2 = new Swiper('.swiper2', {
  autoHeight: true,
  pagination:  {
    el: '.swiper-pagination2',
    clickable: true,
  },
  navigation: {
    nextEl: '.swiper-button-next',
    prevEl: '.swiper-button-prev',
  },
  // Enable debugger
  debugger: true,
});

anyway, thanks a lot for help!无论如何,非常感谢您的帮助!

  1. add another class like:添加另一个类,如:

     class="swiper-container two" ...
  2. append the class in js like:在js中追加类,如:

     new Swiper('.swiper-container.two', { ...

This is how it worked for me这就是它对我有用的方式

Here's a nice easy Vanilla Javascript way of doing it这是一个很好的简单的Vanilla Javascript方法

just change the 'large-slider' class to whatever you're using as your slider name, same with the prevs and next classes for the arrows.只需将“大滑块”类更改为您用作滑块名称的任何内容,与箭头的 prevs 和 next 类相同。

full example here on codepen codepen 上的完整示例

https://codepen.io/davenoham/pen/YzQWdge https://codepen.io/davenoham/pen/YzQWdge

const largeSlider = ()=>{
let largeSliders = document.querySelectorAll('.large-swiper')
let prevArrow = document.querySelectorAll('.prev')
let nextArrow = document.querySelectorAll('.next')
largeSliders.forEach((slider, index)=>{
    let sliderLength = slider.children[0].children.length
    let result = (sliderLength > 1) ? true : false
    const swiper = new Swiper(slider, {
        direction: 'horizontal',
        loop: result,
        navigation: {
            nextEl: nextArrow[index],
            prevEl: prevArrow[index],
        },
        speed: 1000,
    }); 
})

} window.addEventListener('load', largeSlider) } window.addEventListener('load', largeSlider)

var i=1;
var str1='sliderBox_'
var str2='.sliderBox_';
$('.swiper-container').each(function (index,value) {

    var classAdd = str1 + i;
    var classCall = str2 + i;
    var next_slider = 'slider-next' + i;
    var prev_slider = 'slider-prev' + i;
    $(this).addClass(classAdd);
    $(this).parent().siblings(".sites-slider__prev").addClass(prev_slider);
    $(this).parent().siblings(".sites-slider__next").addClass(next_slider);
    new Swiper(classCall,{
        slidesPerView: 4,
        spaceBetween: 20,
        loop: true,
        navigation: {
            nextEl: ('.' + next_slider),
            prevEl: ('.' + prev_slider),
        },
        breakpoints: {
            1199: {
                slidesPerView: 3,
            },
            991: {
                slidesPerView: 2,
            },
            600: {
                slidesPerView: 1,
            },
        }
    });
    i++;



});

You were going down the right path - you need to add a different class to the second swiper but you also need to add references to the new class in dist/js/swiper.min.js otherwise it will only be targeting elements using the original class.您走的是正确的道路 - 您需要向第二个 swiper 添加一个不同的类,但您需要在dist/js/swiper.min.js中添加对新类的引用,否则它只会使用原始元素来定位元素班级。 It should just be a case of copying the relevant sections and repeating them with the new ID (same as what I had to do with wow.js).这应该只是复制相关部分并使用新 ID 重复它们的情况(与我对 wow.js 所做的相同)。

In my case, the issue was with using multiple auto-initialized instances while using Framework7 Core to update slide results based on the data values.就我而言,问题在于使用多个自动初始化实例,同时使用 Framework7 Core 根据数据值更新幻灯片结果。

The missing piece for this use case was assigning an id to the swiper container, for example:这个用例的缺失部分是为 swiper 容器分配一个id ,例如:

<div class="swiper-container swiper-init" id="swiper-instance-{{
userId}}">

This little bit of magic causes everything to work again, otherwise it tries to re-use the Swiper instances and gets very confused about which one the instance belongs to.这一点点魔法让一切都重新开始工作,否则它会尝试重新使用 Swiper 实例并且对实例属于哪个实例感到非常困惑。 Once again Vladimir has thought of everything :)弗拉基米尔再一次想到了一切:)

(Example in CodePen: https://codepen.io/rasaf-ibrahim/pen/mdrVQEq ) (CodePen 中的示例: https ://codepen.io/rasaf-ibrahim/pen/mdrVQEq)

We need to add extra classes on HTML and change classes on CSS and JS to make multiple swiper slideshow work in a single page.我们需要在 HTML 上添加额外的类并在 CSS 和 JS 上更改类,以使多个 swiper 幻灯片在单个页面中工作。

Step-1: Let's first take a look at the HTML files of the swiper slideshow Step-1:我们先看一下swiper幻灯片的HTML文件

<!— HTML of swiper-1 -->

   <div class="swiper-container">
    <div class="swiper-wrapper">
      <div class="swiper-slide" style="background-image:url(https://source.unsplash.com/800x600/?car)"></div>
      <div class="swiper-slide" style="background-image:url(https://source.unsplash.com/800x600/?tree)"></div>
      <div class="swiper-slide" style="background-image:url(https://source.unsplash.com/800x600/?tiger)"></div>
      <div class="swiper-slide" style="background-image:url(https://source.unsplash.com/800x600/?water)"></div>
      <div class="swiper-slide" style="background-image:url(https://source.unsplash.com/800x600/?dog)"></div>
    </div>

    <!-- Swiper - Add Pagination -->
    <div class="swiper-pagination"></div>
  </div>

<!—HTML of swiper-2 -->

<div class="swiper-container ">
   <div class="parallax-bg" style="background-image:url(https://source.unsplash.com/800x600/?space)" data-swiper-parallax="-23%"></div>
  <div class="swiper-wrapper">
    <div class="swiper-slide">
      <div class="title" data-swiper-parallax="-300">Slide 1</div>
      <div class="subtitle" data-swiper-parallax="-200">Subtitle</div>
      <div class="text" data-swiper-parallax="-100">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam dictum mattis velit, sit amet faucibus
          felis iaculis nec. Nulla laoreet justo vitae porttitor porttitor. Suspendisse in sem justo. </p>
      </div>
    </div>
    <div class="swiper-slide">
      <div class="title" data-swiper-parallax="-300" data-swiper-parallax-opacity="0">Slide 2</div>
      <div class="subtitle" data-swiper-parallax="-200">Subtitle</div>
      <div class="text" data-swiper-parallax="-100">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam dictum mattis velit, sit amet faucibus
          felis iaculis nec. Nulla laoreet justo vitae porttitor porttitor. Suspendisse in sem justo. </p>
      </div>
    </div>
    <div class="swiper-slide">
      <div class="title" data-swiper-parallax="-300">Slide 3</div>
      <div class="subtitle" data-swiper-parallax="-200">Subtitle</div>
      <div class="text" data-swiper-parallax="-100">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam dictum mattis velit, sit amet faucibus
          felis iaculis nec. Nulla laoreet justo vitae porttitor porttitor. Suspendisse in sem justo. Integer laoreet
         </p>
      </div>
    </div>
  </div>
  <!-- Add Pagination -->
  <div class="swiper-pagination swiper-pagination-white"></div>
  <!-- Add Navigation -->
  <div class="swiper-button-prev swiper-button-white"></div>
  <div class="swiper-button-next swiper-button-white"></div>
</div>

Step -2: Let's first Identify classes that are common in the CSS file of both swiper slideshow.步骤-2:让我们首先确定两个滑动幻灯片的 CSS 文件中常见的类。

/* CSS of swiper-1 */

   
    .swiper-container {
      width: 100%;
      padding-top: 50px;
      padding-bottom: 50px;
    }

    .swiper-slide {
      background-position: center;
      background-size: cover;
      width: 300px;
      height: 300px;

    }

/* CSS of swiper-2 */

.swiper-container {
    position: relative;
    height: 100%;
    background: #eee;
    font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
    font-size: 14px;
    color: #000;
    margin: 0;
    padding: 0;
  
width: 100%;
  height: 100%;
  background: #000;
}

.swiper-slide {
  font-size: 18px;
  color: white;
  -webkit-box-sizing: border-box;
  box-sizing: border-box;
  padding: 40px 60px;
}

.parallax-bg {
  position: absolute;
  left: 0;
  top: 0;
  width: 130%;
  height: 100%;
  -webkit-background-size: cover;
  background-size: cover;
  background-position: center;

}

.swiper-slide .title {
  font-size: 41px;
  font-weight: 300;
}

.swiper-slide .subtitle {
  font-size: 21px;
}

.swiper-slide .text {
  font-size: 14px;
  max-width: 400px;
  line-height: 1.3;

}

As we can see, .swiper-container and .swiper-slide are common classes我们可以看到, .swiper-container.swiper-slide是常见的类

Step -3: Now, let's identify classes that are common in the JS file of both swiper slideshow.步骤-3:现在,让我们确定两个 swiper 幻灯片的 JS 文件中常见的类。

// JS File of Swiper-1

var swiper = new Swiper('.swiper-container ', {
    effect: 'coverflow',
    grabCursor: true,
    centeredSlides: true,
    slidesPerView: 'auto',
    coverflowEffect: {
      rotate: 50,
      stretch: 0,
      depth: 100,
      modifier: 1,
      slideShadows: true,
    },
    pagination: {
      el: '.swiper-pagination',
    },
  });

// JS file of swiper-2

var swiper = new Swiper('.swiper-container', {
    speed: 600,
    parallax: true,
    pagination: {
      el: '.swiper-pagination',
      clickable: true,
    },
    navigation: {
      nextEl: '.swiper-button-next',
      prevEl: '.swiper-button-prev',
    },
  });

As we can see, .swiper-container and .swiper-pagination are common classes我们可以看到, .swiper-container.swiper-pagination是常见的类

Step-4: Now let's add extra classes on the common classes (.swiper-container .swiper-slide .swiper-pagination) of the HTML file Step-4:现在让我们在 HTML 文件的公共类(.swiper-container .swiper-slide .swiper-pagination)上添加额外的类

<!— HTML of swiper-1 -->

  <div class="swiper-container swiper-container1">
    <div class="swiper-wrapper">
      <div class="swiper-slide swiper-slide1" style="background-image:url(https://source.unsplash.com/800x600/?car)"></div>
      <div class="swiper-slide swiper-slide1" style="background-image:url(https://source.unsplash.com/800x600/?tree)"></div>
      <div class="swiper-slide swiper-slide1" style="background-image:url(https://source.unsplash.com/800x600/?tiger)"></div>
      <div class="swiper-slide swiper-slide1" style="background-image:url(https://source.unsplash.com/800x600/?water)"></div>
      <div class="swiper-slide swiper-slide1" style="background-image:url(https://source.unsplash.com/800x600/?dog)"></div>
      </div>

    <!-- Swiper - Add Pagination -->
    <div class="swiper-pagination swiper-pagination1"></div>
  </div>


    <!—HTML of swiper-2 -->

<div class="swiper-container swiper-container2">
  <div class="parallax-bg" style="background-image:url(https://source.unsplash.com/800x600/?space)" data-swiper-parallax="-23%"></div>
  <div class="swiper-wrapper">
    <div class="swiper-slide  swiper-slide2">
      <div class="title" data-swiper-parallax="-300">Slide 1</div>
      <div class="subtitle" data-swiper-parallax="-200">Subtitle</div>
      <div class="text" data-swiper-parallax="-100">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam dictum mattis velit, sit amet faucibus
          felis iaculis nec. Nulla laoreet justo vitae porttitor porttitor. Suspendisse in sem justo.</p>
      </div>
    </div>
    <div class="swiper-slide swiper-slide2">
      <div class="title" data-swiper-parallax="-300" data-swiper-parallax-opacity="0">Slide 2</div>
      <div class="subtitle" data-swiper-parallax="-200">Subtitle</div>
      <div class="text" data-swiper-parallax="-100">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam dictum mattis velit, sit amet faucibus
          felis iaculis nec. Nulla laoreet justo vitae porttitor porttitor. Suspendisse in sem justo. </p>
      </div>
    </div>
    <div class="swiper-slide swiper-slide2">
      <div class="title" data-swiper-parallax="-300">Slide 3</div>
      <div class="subtitle" data-swiper-parallax="-200">Subtitle</div>
      <div class="text" data-swiper-parallax="-100">
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aliquam dictum mattis velit, sit amet faucibus
          felis iaculis nec. Nulla laoreet justo vitae porttitor porttitor. Suspendisse in sem justo.</p>
      </div>
    </div>
  </div>
  <!-- Add Pagination -->
  <div class="swiper-pagination swiper-pagination2 swiper-pagination-white"></div>
  <!-- Add Navigation -->
  <div class="swiper-button-prev swiper-button-white"></div>
  <div class="swiper-button-next swiper-button-white"></div>
</div>

Step-5: Now let's change those common classes with the newly added classes on both CSS and JS.第 5 步:现在让我们用 CSS 和 JS 上新添加的类来更改这些通用类。

CSS: CSS:

/*  CSS of swiper-1 */

   
    .swiper-container1{
      width: 100%;
      padding-top: 50px;
      padding-bottom: 50px;
    }

    .swiper-slide1 {
      background-position: center;
      background-size: cover;
      width: 300px;
      height: 300px;

    }
  
/* CSS of swiper-2 */

.swiper-container2{
  position: relative;
  height: 100%;
  background: #eee;
  font-family: Helvetica Neue, Helvetica, Arial, sans-serif;
  font-size: 14px;
  color: #000;
  margin: 0;
  padding: 0;

width: 100%;
height: 100%;
background: #000;
}

.swiper-slide2 {
font-size: 18px;
color: white;
-webkit-box-sizing: border-box;
box-sizing: border-box;
padding: 40px 60px;
}

.parallax-bg {
position: absolute;
left: 0;
top: 0;
width: 130%;
height: 100%;
-webkit-background-size: cover;
background-size: cover;
background-position: center;

}

.swiper-slide .title {
font-size: 41px;
font-weight: 300;
}

.swiper-slide .subtitle {
font-size: 21px;
}

.swiper-slide .text {
font-size: 14px;
max-width: 400px;
line-height: 1.3;

}

JS: JS:

//  JS of swiper-1

var swiper = new Swiper('.swiper-container1', {
    effect: 'coverflow',
    grabCursor: true,
    centeredSlides: true,
    slidesPerView: 'auto',
    coverflowEffect: {
      rotate: 50,
      stretch: 0,
      depth: 100,
      modifier: 1,
      slideShadows: true,
    },
    pagination: {
      el: '.swiper-pagination1',
    },
  });


  // JS of swiper-2 

var swiper = new Swiper('.swiper-container2', {
  speed: 600,
  parallax: true,
  pagination: {
    el: '.swiper-pagination2',
    clickable: true,
  },
  navigation: {
    nextEl: '.swiper-button-next',
    prevEl: '.swiper-button-prev',
  },
});

This seem to work for me using in nuxtjs with multiple swipers on page这似乎对我在页面上有多个滑动器的 nuxtjs 中使用有用

used id instead of class in options在选项中使用 id 而不是 class

   <div
      :id="`swiper-button-prev-${element}`"
      class="swiper-button-prev"
    ></div>
    <div
      :id="`swiper-button-next-${element}`"
      class="swiper-button-next"
    ></div>

... Options: ... 选项:

navigation: {
  nextEl: `#swiper-button-next-${this.element}`,
  prevEl: `#swiper-button-prev-${this.element}`,
},

to get unique id, I used为了获得唯一的 ID,我使用了

https://www.npmjs.com/package/uuid https://www.npmjs.com/package/uuid

example of generated output: element: 47bfe557-d75f-455c-9a37-85b7935b297b生成的输出示例:元素:47bfe557-d75f-455c-9a37-85b7935b297b

package.json包.json

"dependencies": {    
    "uuid": "^8.3.2"
 },

on child component, might not be the best way but seem to work在子组件上,可能不是最好的方法,但似乎有效

... ...

<ComponentName v-if="element" />

... ...

import { v4 as uuidv4 } from 'uuid';

... ...

data() {
  return {
    element: null,
  }
}

... ...

mounted() {
  this.element = uuidv4();
}

there is a simple solution for it.有一个简单的解决方案。 you can just create navigation dynamically by enabling {createElements: true}.您可以通过启用 {createElements: true} 来动态创建导航。 It will generate navigation dynamically you also need to enable navigation to add navigation into dom.它将动态生成导航,您还需要启用导航才能将导航添加到 dom。 otherwise nothing will happen否则什么都不会发生

 new Swiper('.color-carousel', { slidesPerView: 8, spaceBetween: 8, loop: true, modules: [Navigation], navigation: true, createElements: true, });

here is the official docs https://swiperjs.com/swiper-api#param-createElements这是官方文档https://swiperjs.com/swiper-api#param-createElements

It is not necessary to increment the button classes.没有必要增加按钮类。

Make sure they are already present in the DOM and that they are children of the parent element declared in the "new Swiper() function".确保它们已经存在于 DOM 中,并且它们是在“new Swiper() 函数”中声明的父元素的子元素 They will work in the context of their parent only.他们将只在父母的背景下工作。

Simply specify your swiper for each use in a page.只需为页面中的每次使用指定您的刷卡器。

Personally, I prefer to target my swipers by unique ids.就个人而言,我更喜欢通过唯一 ID 来定位我的刷卡器。 Something like this:是这样的:

<div class="swiper-container" id="swiper-top">
    <div class="swiper-wrapper">
        <div class="swiper-slide"></div>
        <div class="swiper-slide"></div>
        <div class="swiper-slide"></div>
        <div class="swiper-slide"></div>
    </div>
    <div class="swiper-pagination"></div>
    <button class="swiper-button-prev"></button>
    <button class="swiper-button-next"></button>
</div>

Then use this unique id as an initializer:然后使用这个唯一的 id 作为初始值设定项:

var swiper = new Swiper("#swiper-top", {
    slidesPerView: 4,
    spaceBetween: 12,
    grabCursor: true,
    pagination: {
        el: '.swiper-pagination',
    },
    navigation: {
        prevEl: ".swiper-button-prev",
        nextEl: ".swiper-button-next",
    }
});

Note that my buttons are initialized in the function, although they are already present in the DOM.请注意,我的按钮是在 function 中初始化的,尽管它们已经存在于 DOM 中。 So you don't need to give them a different class each time.所以你不需要每次都给他们一个不同的 class。

Hope you find it useful !希望你觉得它有用 !

 swiperOption: { direction: 'horizontal', slidesPerView: 4, spaceBetween: 6, navigation: { nextEl: '#button-next-relacionados', prevEl: '#button-prev-relacionados' }
 <div id="button-next-relacionados" class="swiper-button-next swiper-button-white"></div> <div id="button-next-relacionados" class="swiper-button-next swiper-button-white"></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM