From 29d012dce08fe61e17a611e4a1cf5182efbacf59 Mon Sep 17 00:00:00 2001 From: Vadim Date: Tue, 20 Apr 2021 12:21:42 +0300 Subject: [PATCH 1/9] Add actions --- src/components/Carousel/Carousel.svelte | 36 +++++++++++++++++++---- src/utils/hoverable.js | 39 +++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 src/utils/hoverable.js diff --git a/src/components/Carousel/Carousel.svelte b/src/components/Carousel/Carousel.svelte index e9b6af8..6248cbb 100644 --- a/src/components/Carousel/Carousel.svelte +++ b/src/components/Carousel/Carousel.svelte @@ -5,6 +5,7 @@ import Arrow from '../Arrow/Arrow.svelte' import { NEXT, PREV } from '../../direction' import { swipeable } from '../../utils/swipeable' + import { hoverable } from '../../utils/hoverable' import { addResizeEventListener, removeResizeEventListener @@ -76,6 +77,16 @@ let offset = 0 let pageWindowElement let pagesElement + let hovered = false + + let autoplayIntervals = [] + $: { + if (hovered) { + clearAutoplay() + } else { + applyAutoplay() + } + } // used for lazy loading images, preloaded only current, adjacent and cloanable images $: loaded = getAdjacentIndexes(originalCurrentPageIndex, originalPagesCount, infinite) @@ -96,15 +107,17 @@ } function applyAutoplay() { - let interval if (autoplay) { - interval = setInterval(() => { + let autoplayInterval = setInterval(() => { directionFnDescription[autoplayDirection]() }, autoplayDuration) + autoplayIntervals.push(autoplayInterval) } - return () => { - interval && clearInterval(interval) - } + } + function clearAutoplay() { + while(autoplayIntervals.length) { + clearInterval(autoplayIntervals.pop()) + } } function addClones() { @@ -128,12 +141,13 @@ infinite && addClones() applyPageSizes() } - cleanupFns.push(applyAutoplay()) + applyAutoplay() addResizeEventListener(applyPageSizes) })() }) onDestroy(() => { + clearAutoplay() removeResizeEventListener(applyPageSizes) cleanupFns.filter(fn => fn && typeof fn === 'function').forEach(fn => fn()) }) @@ -198,8 +212,16 @@ function handleSwipeEnd() { showPage(currentPageIndex, { offsetDelay: 0, animated: true }) } + function handleHovered(event) { + console.log('hovered', event.detail.value) + hovered = event.detail.value + } +
+hovered={hovered} +
+