From 7ce71be462aec12a0ea776f4a0c608e106eeb28c Mon Sep 17 00:00:00 2001 From: linbudu Date: Thu, 1 Feb 2024 20:52:50 +0800 Subject: [PATCH] feat: fix auto play direction in 2 slides --- src/components/ImageGallery.jsx | 70 +++++++++++++++------------------ 1 file changed, 32 insertions(+), 38 deletions(-) diff --git a/src/components/ImageGallery.jsx b/src/components/ImageGallery.jsx index 8b11785a..6f8f15bf 100644 --- a/src/components/ImageGallery.jsx +++ b/src/components/ImageGallery.jsx @@ -1193,8 +1193,8 @@ class ImageGallery extends React.Component { } slideToIndex(index, event) { - const { currentIndex, isTransitioning } = this.state; - const { items, slideDuration, onBeforeSlide } = this.props; + const { currentIndex, isTransitioning, currentSlideOffset } = this.state; + const { items, slideDuration, onBeforeSlide, infinite } = this.props; if (!isTransitioning) { if (event) { @@ -1217,16 +1217,35 @@ class ImageGallery extends React.Component { onBeforeSlide(nextIndex); } - this.setState( - { - previousIndex: currentIndex, - currentIndex: nextIndex, - isTransitioning: nextIndex !== currentIndex, - currentSlideOffset: 0, - slideStyle: { transition: `all ${slideDuration}ms ease-out` }, - }, - this.onSliding - ); + const slideHandler = () => { + this.setState( + { + previousIndex: currentIndex, + currentIndex: nextIndex, + isTransitioning: nextIndex !== currentIndex, + currentSlideOffset: 0, + slideStyle: { transition: `all ${slideDuration}ms ease-out` }, + }, + this.onSliding + ); + }; + + if (infinite && items.length === 2) { + this.setState( + { + // this will reset once index changes + currentSlideOffset: + currentSlideOffset + (currentIndex > index ? 0.001 : -0.001), + slideStyle: { transition: "none" }, // move the slide over instantly + }, + () => { + // add 25ms timeout to avoid delay in moving slides over + window.setTimeout(slideHandler, 25); + } + ); + } else { + slideHandler(); + } } } @@ -1242,36 +1261,11 @@ class ImageGallery extends React.Component { slideTo(event, direction) { const { currentIndex, isTransitioning } = this.state; - const { items } = this.props; const nextIndex = currentIndex + (direction === "left" ? -1 : 1); if (isTransitioning) return; - if (items.length === 2) { - this.slideToIndexWithStyleReset(nextIndex, event); - } else { - this.slideToIndex(nextIndex, event); - } - } - - slideToIndexWithStyleReset(nextIndex, event) { - /* - When there are only 2 slides fake a tiny swipe to get the slides - on the correct side for transitioning - */ - const { currentIndex, currentSlideOffset } = this.state; - this.setState( - { - // this will reset once index changes - currentSlideOffset: - currentSlideOffset + (currentIndex > nextIndex ? 0.001 : -0.001), - slideStyle: { transition: "none" }, // move the slide over instantly - }, - () => { - // add 25ms timeout to avoid delay in moving slides over - window.setTimeout(() => this.slideToIndex(nextIndex, event), 25); - } - ); + this.slideToIndex(nextIndex, event); } handleThumbnailMouseOver(event, index) {