SwiperJS - Previous and Next buttons not working

Viewed 18950

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.

6 Answers

Thank you for your help! The issue for my problem ended up being that I needed to add this to my swiper config:

            observer: true,
            observeParents: true,
            parallax:true,

This is due to the swiper being inside a hidden modal.

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',
  }

Connect the slider in this way and everything will work!

import { Swiper, Parallax, Navigation} from 'swiper'
Swiper.use([ Parallax, Navigation ])

document.addEventListener('DOMContentLoaded', () => {
  const swiper = new Swiper('.swiper-container', {
    // Optional parameters
    direction: 'horizontal',
    loop: false,
    speed: 2400,
    parallax: true,
    loop: false,

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

Just we need to add below in swiper js configuration

observer: true,
observeParents: true,
parallax:true,

So here is full configuration

var swiper = new Swiper('.relatedproducts', {
            
              observer: true,
            observeParents: true,
            parallax:true,
          slidesPerView: 6,
          loop:true,
          autoplay: true,
          simulateTouch:true,
          navigation: {
            nextEl: '.relatedproducts .swiper-button-next',
            prevEl: '.relatedproducts .swiper-button-prev',
          },
        });

This is working for me

Just write it const { swiper } = useSwiper();

that's it. thank you

I also had this issue, but in the scenario, I am manipulating DOM to alter full screen swiper slide display. I tried the configuration method but did not work. I have a grid of images in a small frame, when I click one of them, display the full screen slider and jump to the image.

this.imgs.forEach(img=>{
    img.onclick = () => {
        document.body.appendChild(this.gallery);
        setTimeout(()=>{
            this._swiper.slideTo(this.photos.indexOf(img.related) + 1); 
            // must bind method here
            // otherwise after dismiss swiper next prev will not fire
            this.swiper_prev.onclick = this._swiper.slidePrev.bind(this._swiper);
            this.swiper_next.onclick = this._swiper.slideNext.bind(this._swiper);
        }, 0)
    }
});

note on the setTimeout 0 method, it solved my problem in class constructor. Give it a go if you are in similar situation.

Related