SwiperJS - Previous and Next buttons not working
Asked Answered
O

8

7

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.

Oleg answered 9/6, 2020 at 17:4 Comment(0)
O
8

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.

Oleg answered 11/6, 2020 at 17:49 Comment(0)
A
5

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,

    navigation: {
      nextEl: '.swiper-button-next',
      prevEl: '.swiper-button-prev',
    },
  });
})
Algo answered 25/8, 2021 at 7:16 Comment(0)
A
4

Not trying to revise the previous answers, but hinting an additional tip, especially when upgrading from a previously functional old version to a newer version of Swiper:

The current documentation states the following:

By default Swiper exports only core version without additional modules (like Navigation, Pagination, etc.). So you need to import and configure them too

In my case I needed to add the navigation module to the SwiperOptions of the Swiper instance initialisation after upgrading from an old version to a current one.

import Swiper from 'swiper';
import { Navigation } from 'swiper/modules';  // Don't forget to import Navigation

const swiper = new Swiper(someHTMLElement, {
    modules: [Navigation], // This is the line I needed to add
    navigation: {
        nextEl: '.swiper-button-next',
        prevEl: '.swiper-button-prev',
    },
// your additional config
});

PS: someHTMLElement has to be a CSSSelector or HTMLElement

Allomerism answered 4/9, 2023 at 10:25 Comment(0)
S
2

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',
  }
Seguidilla answered 10/6, 2020 at 13:45 Comment(0)
I
1

My navigation buttons were disabled after loading the page. So added observer: true property. Now is working as expected.

var swiper = new Swiper(".MySwiper", {
slidesPerView: 1,
spaceBetween: 15,
centeredSlides: true,
autoplay: false,
observer: true, // adding this solve my issue
navigation: {
  nextEl: ".swiper-button-next",
  prevEl: ".swiper-button-prev"
 }
});
Ingeingeberg answered 9/5, 2023 at 10:36 Comment(1)
Somehow this solved the issue in my case, thank you!Zavras
P
0

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

Parclose answered 3/6, 2021 at 13:5 Comment(0)
V
0

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

that's it. thank you

Versus answered 7/7, 2022 at 12:26 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Fertile
A
0

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.

Amend answered 8/9, 2022 at 13:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.