Svelte scroll animations
Use @awesome-reveal/svelte to add scroll animations to a Svelte app. An element fades and slides in when it scrolls into view. The package does not link an animation to the scroll position, as a parallax effect or a scroll progress bar does.
Install the package
Section titled “Install the package”pnpm add @awesome-reveal/svelteThe package is a release candidate. For the supported Svelte versions, see Requirements.
Reveal an element on scroll
Section titled “Reveal an element on scroll”Wrap the element in Reveal:
<script> import { Reveal } from "@awesome-reveal/svelte";</script>
<Reveal as="section" from="left"> <h1>Scroll animations in Svelte</h1> <p>The section fades in from the left.</p></Reveal>Reveal renders an element around its children. as sets its tag, and from sets the entrance. To fade, zoom, rotate or blur the element in, set a start state.
Stagger a list with Cascade
Section titled “Stagger a list with Cascade”Cascade reveals its direct children one after the other:
<script> import { Cascade } from "@awesome-reveal/svelte";</script>
<Cascade as="ul" from={{ opacity: 0, y: 16 }} interval={120}> <li>Fast</li> <li>Small</li> <li>Accessible</li></Cascade>For a long list or a tall grid, see Animate a list.
Reveal your own element with an attachment
Section titled “Reveal your own element with an attachment”The reveal and cascade attachments reveal an element that you render, with no wrapper element. Give the options as a function that returns them:
<script> import { reveal } from "@awesome-reveal/svelte";</script>
<article {@attach reveal(() => ({ from: { opacity: 0, scale: 0.95 } }))} class="card"> A card</article>For the options function, see Svelte API.
Attachments run only in the browser. For server-rendered content, use Reveal and Cascade. See SvelteKit.
Control the reveal from state
Section titled “Control the reveal from state”To play the entrance when your state changes, such as when a panel opens, give when. To run code when the element reveals, give onReveal.
Server rendering
Section titled “Server rendering”To prevent a flash of the content before hydration, see Server rendering.
- For SvelteKit, see SvelteKit scroll animations.
- For a Svelte island in Astro, see React, Vue and Svelte scroll animations in Astro.
Next steps
Section titled “Next steps”- Use your own keyframes: give native Web Animations keyframes.
- Change the timing: set the duration, the delay and the threshold.
- Svelte API: all the components, attachments and types.