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

fix (carousel block): fix infinite scroll when slides to show and number of slides are equal #3291

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading