From 36d431e173a590a50ce841dbd6b40d3417cfdca4 Mon Sep 17 00:00:00 2001 From: Vadim Date: Sun, 18 Jul 2021 13:27:06 +0300 Subject: [PATCH] #45 : Update autoplay --- src/components/Carousel/Carousel.svelte | 23 +++++++++++++++-------- src/utils/ProgressManager.js | 1 + 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/src/components/Carousel/Carousel.svelte b/src/components/Carousel/Carousel.svelte index 3b2bf2d..7dd83ab 100644 --- a/src/components/Carousel/Carousel.svelte +++ b/src/components/Carousel/Carousel.svelte @@ -60,11 +60,7 @@ */ export let autoplay = false $: { - if (autoplay) { - applyAutoplay() - } else { - progressManager.reset() - } + applyAutoplayIfNeeded(autoplay) } /** @@ -176,8 +172,19 @@ pagesElement.append(first.cloneNode(true)) } - async function applyAutoplay() { - await autoplayDirectionFnDescription[autoplayDirection]() + async function applyAutoplayIfNeeded(autoplay) { + // prevent progress change if not infinite for first and last page + if ( + !infinite && ( + (autoplayDirection === NEXT && currentPageIndex === pagesCount - 1) || + (autoplayDirection === PREV && currentPageIndex === 0) + ) + ) { + progressManager.reset() + return + } + + autoplay && await autoplayDirectionFnDescription[autoplayDirection]() } let cleanupFns = [] @@ -259,7 +266,7 @@ disabled = false const jumped = await jumpIfNeeded() - !jumped && autoplay && applyAutoplay() // no need to wait it finishes + !jumped && applyAutoplayIfNeeded(autoplay) // no need to wait it finishes } async function showPage(pageIndex, options) { diff --git a/src/utils/ProgressManager.js b/src/utils/ProgressManager.js index e50ded0..2448648 100644 --- a/src/utils/ProgressManager.js +++ b/src/utils/ProgressManager.js @@ -51,5 +51,6 @@ export class ProgressManager { reset() { clearInterval(this.#interval) + this.#onProgressValueChange(1) } }