Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Anthony Ho
Anthony Ho
10,228 Points

adding multiple Swiper.js carousel in one html page

I have a problem with adding more than one carousel

E.g:

var swiper = new Swiper('.swiper-container', {
        pagination: '.swiper-pagination',
        paginationClickable: '.swiper-pagination',
        nextButton: '.swiper-button-next',
        prevButton: '.swiper-button-prev',
        spaceBetween: 30,

    })

var swiper2 = new Swiper('.swiper-container2', {
        pagination: '.swiper-pagination',
        paginationClickable: '.swiper-pagination',
        nextButton: '.swiper-button-next',
        prevButton: '.swiper-button-prev',
        spaceBetween: 30,

    })

Only the first one will be initialised

2 Answers

Andrew Corcoran
Andrew Corcoran
20,552 Points

Sorry, I thought I had tested that piece, apparently not. The easiest way to do that would be to duplicate and modify the classes referenced in your initialization.

    var mySwiper = new Swiper ('.s1', {
      loop: true,
      pagination: '.swiper-pagination',
      nextButton: '.swiper-button-next',
      prevButton: '.swiper-button-prev',
      scrollbar: '.swiper-scrollbar'
    });

So in this case, I took the .swiper-pagination, .swiper-button-next, .swiper-button-prev, and .swiper-scrollbar classes from the original swiper.css file and duplicated them into my custom styles as .swiper-pagination2, .swiper-button-next2, .swiper-button-prev2, and .swiper-scrollbar2.

I've updated the Codepen with these changes and both swipers are working independently of each other: http://corc.co/19VdT

Alternatively, you could simply reference new classes in your initialization and create custom styles. The corresponding classes would need to be updated in your HTML as well, just like they are in my Codepen example.

Anthony Ho
Anthony Ho
10,228 Points

I tested your codepen and it works great. Thanks for your time helping me

Anthony Ho
Anthony Ho
10,228 Points

Another question, How would I append new slides to the slider?

Anthony Ho
Anthony Ho
10,228 Points

When I try to add a third slider, it doesn't work.

Andrew Corcoran
Andrew Corcoran
20,552 Points

Hi Anthony,

I found the answer here: https://github.com/nolimits4web/Swiper/issues/273

And tested it out with Codepen here: http://corc.co/19VdT

Instead of referencing the .swiper-container class, you can add an additional class to your container div and initialize with that instead.

    var mySwiper = new Swiper ('.s1', {
      loop: true,
      pagination: '.swiper-pagination',
      nextButton: '.swiper-button-next',
      prevButton: '.swiper-button-prev',
      scrollbar: '.swiper-scrollbar'
    });
    var mySwiper2 = new Swiper ('.s2', {
      loop: true,
      pagination: '.swiper-pagination',
      nextButton: '.swiper-button-next',
      prevButton: '.swiper-button-prev',
      scrollbar: '.swiper-scrollbar'
    });
  <div class="swiper-container s1">
    <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>
    <div class="swiper-pagination"></div>
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>
    <div class="swiper-scrollbar"></div>
  </div>

  <div class="swiper-container s2">
    <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>
    <div class="swiper-pagination"></div>
    <div class="swiper-button-prev"></div>
    <div class="swiper-button-next"></div>
    <div class="swiper-scrollbar"></div>
  </div>
.s1 {
    width: 600px;
    height: 300px;
}

.s2 {
    width: 600px;
    height: 300px;
} 

You could continue to use the .s1 and .s2 classes to style your sliders, and even add additional classes for styling to your wrappers, slides, pagination, etc. This will prevent accidentally styling one slider when you meant to style the other.

Anthony Ho
Anthony Ho
10,228 Points

the problem is not the styling. I want slider to be different, as in each slider has to be individual. Right now, when I click the next arrow on the first slider, it will also move the second slider. This is because s1 and s2 share the same classes for pagination, nextButton and prevButton.