diff --git a/src/components/Carousel/Carousel.svelte b/src/components/Carousel/Carousel.svelte index 986bda0..bb401b1 100644 --- a/src/components/Carousel/Carousel.svelte +++ b/src/components/Carousel/Carousel.svelte @@ -199,6 +199,7 @@ // used for lazy loading images, preloaded only current, adjacent and cloanable images $: loaded = getAdjacentIndexes({ + infinite, scrollIndex: currentScrollIndex, scrollsCount, pagesCount: pagesCountWithoutClones, @@ -276,28 +277,7 @@ pagesToShow, pagesCountWithoutClones, }) - // load first and last child to clone them - scrollsCount = getScrollsCount({ - infinite, - pagesCountWithoutClones, - pagesToScroll, - }) - - loaded = [ - ...getIndexesOfPagesWithoutClonesInScroll({ - scrollIndex: 0, - pagesToShow, - pagesToScroll, - pagesCount: pagesCountWithoutClones, - }), - ...getIndexesOfPagesWithoutClonesInScroll({ - scrollIndex: scrollsCount - 1, - pagesToShow, - pagesToScroll, - pagesCount: pagesCountWithoutClones, - }), - ] await tick() infinite && addClones() diff --git a/src/utils/page.js b/src/utils/page.js index bf1c56a..8d58cc3 100644 --- a/src/utils/page.js +++ b/src/utils/page.js @@ -77,18 +77,34 @@ export function getIndexesOfPagesWithoutClonesInScroll({ } export function getAdjacentIndexes({ + infinite, scrollIndex, scrollsCount, pagesCount, pagesToShow, pagesToScroll, }) { - // not checking is infinite or not, as first and last scrolls are always shown to be cloned const _scrollIndex = Math.max(0, Math.min(scrollIndex, scrollsCount - 1)) - const rangeStart = Math.max(0, _scrollIndex - 1) - const rangeEnd = Math.min(_scrollIndex + 1, scrollsCount - 1) - const scrollIndexes = [...new Set([rangeStart, rangeEnd, _scrollIndex])].sort((a, b) => a - b) + let rangeStart = _scrollIndex - 1 + let rangeEnd = _scrollIndex + 1 + rangeStart = rangeStart < 0 + ? infinite + ? scrollsCount - 1 + : 0 + : rangeStart + rangeEnd = rangeEnd > scrollsCount - 1 + ? infinite + ? 0 + : scrollsCount - 1 + : rangeEnd + + const scrollIndexes = [...new Set([ + rangeStart, + _scrollIndex, + rangeEnd, + scrollsCount - 1, // needed to clone last scroll pages + ])].sort((a, b) => a - b) const pageIndexes = [] scrollIndexes.forEach(scrollIndex => pageIndexes.push( ...getIndexesOfPagesWithoutClonesInScroll({