From 0408df319eec9a21d5aab8832410f7adfe44a7e3 Mon Sep 17 00:00:00 2001 From: Vadim Date: Thu, 21 Jan 2021 10:27:00 +0300 Subject: [PATCH] Fix changing items --- src/CarouselChild.svelte | 21 +++++++++++++----- src/ImageCarousel.svelte | 32 +++++++++++++++++++++++++--- src/store.js | 21 ++++++++++++------ src/stories/ImageCarouselView.svelte | 28 +++++++++++++++++------- src/transition.js | 16 ++++++++++++++ 5 files changed, 96 insertions(+), 22 deletions(-) create mode 100644 src/transition.js diff --git a/src/CarouselChild.svelte b/src/CarouselChild.svelte index 83af966..d43e876 100644 --- a/src/CarouselChild.svelte +++ b/src/CarouselChild.svelte @@ -1,31 +1,42 @@ -{#if id === $store.currentItemId}
-{/if } diff --git a/src/ImageCarousel.svelte b/src/ImageCarousel.svelte index 8cb6181..7ead97f 100644 --- a/src/ImageCarousel.svelte +++ b/src/ImageCarousel.svelte @@ -14,16 +14,28 @@ export let infinite = true; let contentContainerElement + let innerContentContainerElement let children + let w = 0 onMount(() => { - children = contentContainerElement.children + children = innerContentContainerElement.children + console.log('children', children.length) + w = contentContainerElement.clientWidth + for (let i=0; i @@ -36,8 +48,16 @@ >< {/if} -
- +
+
+ +
{#if arrows}
@@ -57,6 +77,12 @@ .content-container { flex: 1; display: flex; + overflow: hidden; + } + .content-container > div { + display: flex; + transition: transform 1s ease-in-out; + background-color: chocolate; } .side-container { background-color: cornflowerblue; diff --git a/src/store.js b/src/store.js index fe22800..44e3f4d 100644 --- a/src/store.js +++ b/src/store.js @@ -1,16 +1,21 @@ import { writable } from 'svelte/store'; +import { v4 as uuid } from 'uuid' import { getNextItemIndexFn, getPrevItemIndexFn } from './utils/item-index' + function createStore() { const { subscribe, set, update } = writable({ items: [], - currentItemId: null + currentItemId: null, + currentItemIndex: null, + action: 'next' }); - function setItem(id) { + function setItem(id = uuid()) { update(store => ({ ...store, currentItemId: id, + currentItemIndex: 0, // store.items.length - 1, TODO: use as a param items: [ ...store.items, id @@ -27,22 +32,26 @@ function createStore() { function next({ infinite }) { update(store => { - const currentItemIndex = store.items.findIndex(item => item === store.currentItemId) + const currentItemIndex = store.currentItemIndex // store.items.findIndex(item => item === store.currentItemId) const newCurrentItemIndex = getNextItemIndexFn(infinite)(currentItemIndex, store.items) return { ...store, - currentItemId: store.items[newCurrentItemIndex] + currentItemId: store.items[newCurrentItemIndex], + currentItemIndex: newCurrentItemIndex, + action: 'next' } }) } function prev({ infinite }) { update(store => { - const currentItemIndex = store.items.findIndex(item => item === store.currentItemId) + const currentItemIndex = store.currentItemIndex // store.items.findIndex(item => item === store.currentItemId) const newCurrentItemIndex = getPrevItemIndexFn(infinite)(currentItemIndex, store.items) return { ...store, - currentItemId: store.items[newCurrentItemIndex] + currentItemId: store.items[newCurrentItemIndex], + currentItemIndex: newCurrentItemIndex, + action: 'prev' } }) } diff --git a/src/stories/ImageCarouselView.svelte b/src/stories/ImageCarouselView.svelte index 3d7b131..913f49b 100644 --- a/src/stories/ImageCarouselView.svelte +++ b/src/stories/ImageCarouselView.svelte @@ -18,16 +18,28 @@ {arrows} {infinite} > - -
+ +

Element 1

- - -
+ +

Element 2

- + +
+

Element 3

+
+
@@ -37,8 +49,8 @@ width: 100%; } .child-content-container { - width: 100%; - display: flex; + width: 500px; + height: 100px; align-items: center; justify-content: center; } diff --git a/src/transition.js b/src/transition.js new file mode 100644 index 0000000..861b4f5 --- /dev/null +++ b/src/transition.js @@ -0,0 +1,16 @@ +import { cubicIn } from 'svelte/easing'; + +export function custom(node, { duration, offset }) { + console.log(node.style.left) + return { + duration, + css: t => { + const eased = cubicIn(t); + // console.log(Math.round(offset/t)) + // transform: translate(${Math.round(offset/t)}px); + return ` + transform: translate(${Math.round(offset/t)}px); + ` + } + }; +}