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

Be able to set a number of duplicated slides to fix the new loop behavior on certain scenarios #7254

Closed
3 of 4 tasks
XavierCrespin opened this issue Dec 12, 2023 · 6 comments

Comments

@XavierCrespin
Copy link

XavierCrespin commented Dec 12, 2023

Clear and concise description of the problem

As can be seen here : Swiper V11 loop issue and since the V9 update that changed the loop behavior, in design cases where the slides overflow the main container, there is not enough duplicated slides to cover these specific needs.
Worse, when slidesPerView = number of slides + 1, sometimes the center container only shows slidesPerView - 1,
in the above codepen example, the slidesPerView is set to 3 and when going back and forth, sometimes there are only 2 slides in the center container.
Here is an example of what I can achieve with V8 : Swiper V8 loop works

Suggested solution

A new parameter like "numberOfDuplicateSlides" to cover these specific design scenarios

Alternative

Stick to V8 for the moment on all impacted websites (unfortunately, a few).
I could duplicate manually the number of slides to artificially fix the issue, but this would break the pagination.

Additional context

No response

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.

Would you like to open a PR for this feature?

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

There is a loopAdditionalSlides parameter for that

@XavierCrespin XavierCrespin changed the title Be able to set a number of duplicated slides or groups to fix the new loop behavior on certain scenarios Be able to set a number of duplicated slides to fix the new loop behavior on certain scenarios Dec 12, 2023
@XavierCrespin
Copy link
Author

Well, loopAdditionalSlides just does not work as intended then

@rajnikakadiya
Copy link

@nolimits4web loopAdditionalSlides doesn't works as inteded because it moves slides from left to right but then left side becomes empty. There should be option numberOfDuplicateSlides like it was in older version.

@MartinTale
Copy link

Could really use this feature - loopAdditionalSlides doesn't do the trick :)

@kubiqsk
Copy link

kubiqsk commented Apr 11, 2024

I'm also looking for solution as swiper from version 9+ broke a lot of sliders that simply need duplicated slides.

Is there some additional library or some extension that can fix this?

@kubiqsk
Copy link

kubiqsk commented Apr 11, 2024

Ok, here is my very tricky-hacky solution...
This is just a first try and it works in my scenario, but I'm sure there are many usecases when it will not work...
I'm using this in on.init and on.resize

// duplicate slides if needed
const swiperWidth = swiperEl.swiper.size;
if( ! swiperEl.originalSlidesWidth ){
	swiperEl.originalSlidesWidth = swiperEl.swiper.slidesSizesGrid.reduce( ( accumulator, currentValue ) => accumulator + currentValue, 0 );
}
const allSlidesWidth = swiperEl.swiper.slidesSizesGrid.reduce( ( accumulator, currentValue ) => accumulator + currentValue, 0 );
const smallestSlide = Math.min( ...swiperEl.swiper.slidesSizesGrid );
// if all slides are smaller than swiper area and smallest slide
if( swiperWidth + smallestSlide > allSlidesWidth ){
	// calculate needed duplicates
	const existingDuplicates = Math.round( ( allSlidesWidth - swiperEl.originalSlidesWidth ) / swiperEl.originalSlidesWidth );
	const duplicatesNeeded = Math.ceil( ( swiperWidth + smallestSlide ) / swiperEl.originalSlidesWidth ) - 1 - existingDuplicates;
	// get all slides html
	if( ! swiperEl.allSlidesHTML ){
		swiperEl.allSlidesHTML = swiperEl.swiper.slides.reduce( ( accumulator, currentValue ) => accumulator + '<div class="swiper-slide swiper-slide-duplicate">' + currentValue.innerHTML + '</div>', '' );
	}
	// add duplicated slides
	for( let i = 0; i < duplicatesNeeded; i++ ){
		swiperEl.swiper.slidesEl.insertAdjacentHTML( 'beforeend', swiperEl.allSlidesHTML );
	}
	// update swiper
	swiperEl.swiper.update();
}

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

5 participants