sveltersect is an ultra-lightweight library that lets you effortlessly animate elements when they come in/out of view. Use built-in Svelte transitions, CSS classes, or custom callbacks to create stunning effects with minimal effort.
Other uses:
npm i -D sveltersect
sveltersect has three main ways to animate elements:
Use the built-in Svelte transitions. You can use any of the built-in transitions or create your own.
<script>
import { fade } from 'svelte/transition';
import { reveal } from 'sveltersect';
</script>
<div use:reveal={{ transition: { animation: fade, threshold: 0.3 } }}>
This element will fade in when it enters the viewport and fade out when it exits.
</div>
sveltersect will automatically apply the correct transition in each state; mount, unmount, enter, and exit.
Prefer CSS? Simply pass a class (or classes) to tie it into the observer.
<div use:reveal={{ class: 'fade-in-out', transition: { threshold: 0.5 } }}>
This element will fade in when it enters the viewport and fade out when it exits.
</div>
<style>
div {
opacity: 0;
transition: opacity 0.5s ease-in-out;
}
.fade-in-out {
opacity: 1;
}
</style>
—just remember to set the initial state in your CSS.
<div use:reveal={{ callbacks: {
enter: () => console.log('enter'),
exit: () => console.log('exit')
}}}>
This element will log to the console when it enters and exits the viewport.
</div>
Lastly, you can also use separate parameters for entering and exiting transitions:
<div use:reveal={{
in: { animation: fly, params: { y: 100 }, threshold: 0.5 },
out: { animation: fade, params: { duration: 200 }, threshold: 0.5 }
}}>
This element will fade in when it enters the viewport and fade out when it exits.
</div>
For an extensive list of options, check out the type declarations.
Third party libraries are tricky; you need to tickle just the right spots to get the desired effect. Because of this, there is no one-size-fits-all solution. Here's an exampel using number-flow
:
<script>
import NumberFlow from '@number-flow/svelte';
let value = $state(123);
function setValue() {
value = 123;
};
function resetValue() {
value = 0;
};
</script>
<div use:reveal={{
callbacks: {
enter: setValue,
exit: resetValue,
},
transition: {
threshold: 0.3,
},
}}>
<NumberFlow {value} duration={1000} />
</div>
This will create the following effect
0
123
0
You can change the mounting behavior just by changing the initial value. For example, using let value = $state(0);
, we get the following:
123
0
You may need to also set initial: true
, which will cause setValue
to be called on mount.
To use the generic type for reveal
options, do the following:
const config = {
in: {
animation: fade,
params: { duration: 800 },
},
out: {
animation: fly,
params: { duration: 800, x: 0 },
},
} satisfies SveltersectOptions<typeof fade, typeof fly>;
—or use the helper function to avoid the generic:
const config2 = defineRevealOptions({
in: {
animation: fade,
params: { duration: 800 },
},
out: {
animation: fly,
params: { duration: 800, x: 0 },
},
})
Use the <Reveal>
Svelte component to wrap any content:
<script>
import { fade } from 'svelte/transition';
const opts = defineRevealOptions({
transition: { animation: blur, threshold: 0.3 },
initial: true,
});
</script>
<Reveal {...opts}>
<p>Component demo</p>
</Reveal>
Advantages:
reveal
actionSveltersect was created amongst many other libraries that do the same thing. My main goal was to create library that is:
That aside, Sveltersect should be capable of doing everything the other libraries can do, and more. If you find a bug or have a feature request, please open an issue.