Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

147
Views
Initialize multiple swipers.js on a single page by using Vanilla JS

First of all, the answers I found on this forum didn't solve my problem.

I'm using Swiper 8.0.7.

When I use classes below, only the one with class "slider-main__body" gets intitialized. If use a common class "swiper" they both include, the both swipers are initialized, however when I use controls (arrows and pagination) on the second slider, the images of the first one are moving. I found a few helpful suggestions, but none of them worked. I will appreciate your help.

function initSliders() {
    if (document.querySelector('.slider-main__body')) { 
        new Swiper('.slider-main__body', { 
            modules: [Navigation, Pagination],
            observer: true,
            observeParents: true,
            slidesPerView: 1,
            spaceBetween: 32,
            autoHeight: true,
            speed: 800,
            loop: true,
            loopAdditionalSlides: 5,
            preloadImages: false,
            parallax: true,

            pagination: {
                el: '.controls-slider-main__dotts.swiper-pagination',
                clickable: true,
            },
            
            navigation: {
                prevEl: '.swiper-arrow.swiper-arrow-prev',
                nextEl: '.swiper-arrow.swiper-arrow-next',
            },
        });
    }           
    if (document.querySelector('.slider-rooms__body')) { 
        new Swiper('.slider-rooms__body', { 
            modules: [Navigation, Pagination],
            observer: true,
            observeParents: true,
            slidesPerView: auto,
            spaceBetween: 24,
            autoHeight: true,
            speed: 800,
            loop: true,
            loopAdditionalSlides: 5,
            preloadImages: false,
            parallax: true,

            pagination: {
                el: '#swiper-pagination',
                clickable: true,
            },

            navigation: {
                prevEl: '#swiper-arrow-prev',
                nextEl: '#swiper-arrow-next',
            },
        });
    }
}

window.addEventListener("load", function (e) {
        initSliders();
    });
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!