A reusable animate-on-scroll component for SvelteKit
Originally written in June 2022. The code below is Svelte 4 (export let, <slot>) and predates runes.
Ever needed to animate in an element once it's visible on the screen? There are multiple ways to achieve this, with even more ways to handle the animation. I'm using svelte-intersection-observer to track when the element is in view, and TailwindCSS classes for the animations.
Check out the code: <AnimatedElement>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
I know this code could be shortened, but I'm not that concerned with that. I'd rather have a component I can easily read. Also note, I'm passing intersecting back into the slot, in case you need the trigger wherever you use the component
How to use:
|
|
|
|
|
Potential improvements
I didn't have time to lollygag when coding this, but I would like to make this without needing the extra wrapper div inside the <AnimatedElement> component. I don't see why this wouldn't be possible by using the <svelte:element this={tag}> tag and passing the element tag along with the class into the component. Like this <AnimatedElement tag="div" class="flex">. Let me know you'd like for me to code that out.
Thanks for reading! Now go grab some pizza!