SwiperJS - Previous and Next buttons not working

Justin picture Justin · Jun 9, 2020 · Viewed 7.5k times · Source

I am working with SwiperJS (www.swiperjs.com), and I am having an issue where the swipe to the next photo is working, but the previous and next buttons are not. I have looked at every article I can find on here as well as following their documentation, and still not having any luck.

    <div id="openModal" class="modalDialog" style="display: none;">
    <div class="modal-content">
        <a href="#close" title="Close" class="close" onclick="$('#openModal').hide()">X</a>
        <h2>Modal Box</h2>
        <p>Hello world</p>
        <script src="https://unpkg.com/swiper/js/swiper.min.js"></script>
        <link rel="stylesheet" href="https://unpkg.com/swiper/css/swiper.css">
        <link rel="stylesheet" href="https://unpkg.com/swiper/css/swiper.min.css">
        <!-- Slider main container -->
        <div class="swiper-container">
            <!-- Additional required wrapper -->
            <div id="swiper" class="swiper-wrapper">
                <!-- Slides -->
                <div class="swiper-slide"><img src="http://<server>/path/to/image.png"></div>
                <div class="swiper-slide"><img src="http://<server>/path/to/image2.png"></div>
            </div>
            <!-- If we need pagination -->
            <div class="swiper-pagination"></div>

            <!-- If we need navigation buttons -->
            <div class="swiper-button-prev"></div>
            <div class="swiper-button-next"></div>

            <!-- If we need scrollbar -->
            <div class="swiper-scrollbar"></div>
        </div>
        <script>
        var mySwiper = new Swiper ('.swiper-container', {
            // Optional parameters
            direction: 'horizontal',
            loop: true,
            centeredSlides: true,
            slidesPerView: 1,

            pagination: '.swiper-pagination',
            paginationClickable: '.swiper-pagination',
            nextButton: '.swiper-button-next',
            prevButton: '.swiper-button-prev',
        })
        </script>

For initializing the Swiper JS I have also tried this right from their website:

  <script>
  var mySwiper = new Swiper ('.swiper-container', {
    // Optional parameters
    direction: 'vertical',
    loop: true,

    // If we need pagination
    pagination: {
      el: '.swiper-pagination',
    },

    // Navigation arrows
    navigation: {
      nextEl: '.swiper-button-next',
      prevEl: '.swiper-button-prev',
    },

    // And if we need scrollbar
    scrollbar: {
      el: '.swiper-scrollbar',
    },
  })
  </script>

Does anybody have any ideas on what I could try? I also found that the pagination buttons are not showing at the bottom like in their example, but that is less of an issue compared to the previous and next buttons not working. Like I said, if I click and drag or "swipe," it moves just fine to the next slide, so I am assuming its related to the JS section, but not sure what to try.

Answer

Yaakov David picture Yaakov David · Jun 10, 2020

In the first section of code you posted change the JS you have to

<script>
  var mySwiper = new Swiper ('.swiper-container', {
      // Optional parameters
      direction: 'horizontal',
      loop: true,
      centeredSlides: true,
      slidesPerView: 1,

      pagination: {
        el: '.swiper-pagination',
        clickable: true,
      },
      navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
      }
  })
  </script>

and both the arrows and the pagination should work.

What I changed is

pagination: '.swiper-pagination',
paginationClickable: '.swiper-pagination',


To

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


And

nextButton: '.swiper-button-next',
prevButton: '.swiper-button-prev',


To

navigation: {
    nextEl: '.swiper-button-next',
    prevEl: '.swiper-button-prev',
  }