Minimal example of how Svelte Scroller works.
bind
or as one-way binding):
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
This is the background content. It will stay fixed in place while the foreground scrolls over the top.
Index: 0, i.e. section #0 is currently active
Offset: 0
Progress: 0
Count: 0
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Svelte Scroller with a D3 graph where the graph updates happen from D3 based on index of steps.
console.log(index)
, which will produce undefined
,
as opposed to $: console.log(index)
, which will console-log the index exactly once each time it updates.
By the same logic, any graph props that use any of these values need to be defined reactively.
If you want to test this,
add a console.log(index)
in the Graph code logic, in onMount
and in afterUpdate
. You will see that onMount
will return undefined
, while afterUpate
will return many many console logs,
rather than only when the index value actually changes.
Only the reactive statement, $: console.log(index)
,
will log the index as expected - exactly once each time it changes,
which in this case is each time the next or previous step enters the correct place in the viewport.
$: props = ...
but then either only calling all your graph-creation,
update and drawing logic from onMount
or from afterUpdate
or from both.
This is no good because using onMount
alone and then nothing else will give the graph no
chance to update its props, so it can't take in the index as a parameter and draw different things depending
on the index. Using afterUpdate
is also no good because that will keep firing every time anything
updates and so it will overfire and pass in the index prop or other props to the graph way too often. This is exactly what we want to avoid.
So, in conclusion, onMount
will not allow for updates based on props, while afterUpdate
will unintentionally create too many updates.
$: props = ...
onMount
, passing it the container, (initial) props and (initial) data.
$: graph.props(props).draw()
display: flex
.
top=0
. It becomes unstuck when the bottom of the background container hits the top of the viewport.
This corresponds to bottom=1
.
height: 50vh
, which makes the start of step 1 align with the center of the viewport.
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
Svelte Scroller with a D3 graph using GSAP to transition on ScrollTrigger-based steps. Running vanilla JS logic, targetting DOM elements with query selectors, from inside of Svelte.
index
variable that can be
passed in as a prop from Svelte Scroller or write any conditional on index
code for the graph state inside of Svelte.
Instead, we give each step's DOM element (here a div) a unique id and use this
to target the step entirely from the vanilla JS-based GSAP ScrollTrigger code.
The graph state for each step is still controlled from D3.
stepOneLogic()
, and then call all of them as callbacks to the onToggle
method of the ScrollTrigger
when the correct step is hit.
We call all of them from the same method gsapAnimate()
.
Note that each step becomes active and displays the graph state for that step
when the top of the step hits the center of the viewport on scrolling down, but when the
bottom of the step enters the viewport when scrolling back up.
onMount
(which has to check that the width and height variables exist).
However, then it wouldn't be responsive, as it only draws the graph once and doesn't update when the width and height
update. The reactive declaration approach does that.