A lightweight, dependency-free, and performant Svelte 5 component for tracking scroll progress of an element within the viewport.
This component is intentionally barebones: it generates only a single CSS progress variable (--scroll-progress
), making it extremely flexible and easy to apply to any CSS animation, effect, or logic you want. You have full control over how to use the progress value in your styles or scripts.
requestAnimationFrame
, ensuring updates are efficient and do not overload the browser during rapid scrolling.npm install svelte-scroll-tracker
<script lang="ts">
import ScrollTracker from 'svelte-scroll-tracker';
</script>
<ScrollTracker
startThreshold={0}
endThreshold={0.5}
debug={false}
>
<div style="height: 400px;">
Scroll me!
<div>Progress: {Math.round(100 * $css('--scroll-progress'))}%</div>
</div>
</ScrollTracker>
--scroll-progress
(from 0 to 1) on its root element as you scroll.startThreshold
, endThreshold
, and debug
via props.You can also use the progress
value programmatically via the children
snippet slot:
<ScrollTracker>
{#snippet children(progress)}
static content
{#if progress > 0.5}
dynamic content
{/if}
{/snippet}
</ScrollTracker>
Prop | Type | Default | Description |
---|---|---|---|
startThreshold | number | 0 | When to start mapping progress (0-1) |
endThreshold | number | 0.5 | When to end mapping progress (0-1) |
debug | boolean | false | Enable debug logging |