Skip to content

Commit

Permalink
fix (carousel block): jumping slides on infinite scroll when slides t…
Browse files Browse the repository at this point in the history
…o show and number of slides are equal (#3291)
  • Loading branch information
mxkae authored Sep 5, 2024
1 parent 7308fa0 commit da1bfb8
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/block/carousel/frontend-carousel.js
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,10 @@ class _StackableCarousel {

swapSlides = ( slide, dir ) => {
let setScrollToClone = false
if ( this.slidesToShow === this.slideEls.length ) {
setScrollToClone = true
}

if ( dir === 'N' && slide > this.slideEls.length ) {
slide = this.slideOffset
setScrollToClone = true
Expand All @@ -242,6 +246,16 @@ class _StackableCarousel {

original.map( node => this.sliderEl.insertBefore( node, this.clones[ needToSwap ] ) )
clones.map( node => this.sliderEl.insertBefore( node, this.slideEls[ needToSwap ] ) )

// This ensures that the cloned slides are in the right position when slides to show === number of slides
if ( this.slidesToShow === this.slideEls.length && dir === 'N' ) {
const children = this.sliderEl.children
this.sliderEl.append( children[ 0 ] )
} else if ( this.slidesToShow === this.slideEls.length && dir === 'P' ) {
const children = [ ...Array.from( this.sliderEl.children ).slice( -2 ) ].reverse()
children.map( node => this.sliderEl.insertBefore( node, this.sliderEl.children[ 0 ] ) )
}

this.swappedSlides = needToSwap
} else if ( this.swappedSlides > needToSwap ) {
// unswap original and clone slides that are not needed
Expand All @@ -251,6 +265,12 @@ class _StackableCarousel {
original.map( node => this.sliderEl.insertBefore( node, this.slideEls[ this.swappedSlides ] ) )
clones.map( node => this.sliderEl.insertBefore( node, this.clones[ this.swappedSlides ] ) )
this.swappedSlides = _needToSwap

// This ensures that the cloned slides are in the right position when slides to show === number of slides
if ( this.slidesToShow === this.slideEls.length ) {
const children = this.sliderEl.children
this.sliderEl.insertBefore( children[ children.length - 1 ], children[ 0 ] )
}
}

if ( setScrollToClone ) {
Expand Down

0 comments on commit da1bfb8

Please sign in to comment.