Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Swiper Infinite loop scroll jumping #7270

Open
5 of 6 tasks
deividisss opened this issue Dec 21, 2023 · 6 comments
Open
5 of 6 tasks

Swiper Infinite loop scroll jumping #7270

deividisss opened this issue Dec 21, 2023 · 6 comments

Comments

@deividisss
Copy link

Check that this is really a bug

  • I confirm

Reproduction link

https://codesandbox.io/p/sandbox/yl6ddq

Bug description

As an example, I will use the Slider Infinite Loop demo with 9 slides: https://swiperjs.com/demos/200-infinite-loop/core.
When you press the back button arrow to go from the first slide to the last slide and then press pagination dot to go from slide 9 to slide 8, you will notice a crazy jump through multiple slides, and it keeps happening when you switch between slides 8 and 9 using pagination dots.

Expected Behavior

Slider should scroll one slide

Actual Behavior

It scrolls multiple slides
https://github.com/nolimits4web/swiper/assets/48391144/0392a2c5-9847-44dd-9076-e4aed34486f5

Swiper version

v11.0.5

Platform/Target and Browser Versions

Windows /Linux Version 120.0.6099.111 (Official Build) (64-bit)

Validations

  • Follow our Code of Conduct
  • Read the docs.
  • Check that there isn't already an issue that request the same feature to avoid creating a duplicate.
  • Make sure this is a Swiper issue and not a framework-specific issue

Would you like to open a PR for this bug?

  • I'm willing to open a PR
@hajirasyafi
Copy link

Yeah I can see it

@hamzaejaz787
Copy link

I have same issue, only happens when using navigation to go from last to first then i clicking on any pagination in between and it jumps like crazy. Any help would be appriciated

@rengarcia
Copy link

Same here, I have the similar error :(

@and-t
Copy link

and-t commented Apr 3, 2024

I wrote a quick fix to this issue, after dealing with it on production. For reference, my swiper configuration:

const swiper = new Swiper(".swiper", {
    modules: [Navigation, Pagination],
    slidesPerView: "auto",
    centeredSlides: true,
    spaceBetween: 15,
    grabCursor: true,
    loop: true,
    slidesPerGroup: 1,
    loopFillGroupWithBlank: false,
    loopAdditionalSlides: 2,
    lazy: {
        loadPrevNext: true,
        loadPrevNextAmount: 2,
    },
    keyboard: {
        enabled: true,
    },
    navigation: {
        nextEl: ".swiper-button-next",
        prevEl: ".swiper-button-prev",
    },
    pagination: {
        el: ".swiper-pagination",
        clickable: true,
    },
});

I really wanted a centeredSlides: true swiper with slidesPerView: "auto", but this meant that going backwards past index: 0 would generate a weird behavior (there would be a full loop, or jumping a few slides / indexes). Taking the slidesPerView: "auto" fixed the backwards jump, but now there was an issue going forward. So I wrote the following fix:

  1. First, you need a global (or scoped, depending on your code) variable:

let currentIndex = 0;

  1. Then, we need two custom functions for each swiper navigation button:
const prevButton = document.querySelector(".swiper-button-prev");
const nextButton = document.querySelector(".swiper-button-next");

prevButton.addEventListener("click", () => {
    if (currentIndex === 0) {
        currentIndex = swiper.slides.length - 1;
    } else {
        currentIndex--;
    }
    swiper.slideToLoop(currentIndex);
    console.log(currentIndex);
});

nextButton.addEventListener("click", () => {
    currentIndex = swiper.realIndex;
    console.log(currentIndex);
});

This will eliminate the issue of the realIndex jumping like crazy when going backwards. If your issue appears when going forward on the slide, swap the functions.

@shalevc1098
Copy link

I wrote a quick fix to this issue, after dealing with it on production. For reference, my swiper configuration:

const swiper = new Swiper(".swiper", {
    modules: [Navigation, Pagination],
    slidesPerView: "auto",
    centeredSlides: true,
    spaceBetween: 15,
    grabCursor: true,
    loop: true,
    slidesPerGroup: 1,
    loopFillGroupWithBlank: false,
    loopAdditionalSlides: 2,
    lazy: {
        loadPrevNext: true,
        loadPrevNextAmount: 2,
    },
    keyboard: {
        enabled: true,
    },
    navigation: {
        nextEl: ".swiper-button-next",
        prevEl: ".swiper-button-prev",
    },
    pagination: {
        el: ".swiper-pagination",
        clickable: true,
    },
});

I really wanted a centeredSlides: true swiper with slidesPerView: "auto", but this meant that going backwards past index: 0 would generate a weird behavior (there would be a full loop, or jumping a few slides / indexes). Taking the slidesPerView: "auto" fixed the backwards jump, but now there was an issue going forward. So I wrote the following fix:

  1. First, you need a global (or scoped, depending on your code) variable:

let currentIndex = 0;

  1. Then, we need two custom functions for each swiper navigation button:
const prevButton = document.querySelector(".swiper-button-prev");
const nextButton = document.querySelector(".swiper-button-next");

prevButton.addEventListener("click", () => {
    if (currentIndex === 0) {
        currentIndex = swiper.slides.length - 1;
    } else {
        currentIndex--;
    }
    swiper.slideToLoop(currentIndex);
    console.log(currentIndex);
});

nextButton.addEventListener("click", () => {
    currentIndex = swiper.realIndex;
    console.log(currentIndex);
});

This will eliminate the issue of the realIndex jumping like crazy when going backwards. If your issue appears when going forward on the slide, swap the functions.

how can i fix it with react?

@joey-ma
Copy link

joey-ma commented May 10, 2024

still an issue today. Tried to implement this with solution proposed in thread @and-t, but couldn't produce the desired fix :( (I'm also using React)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

8 participants