It's not real - it's just magic
This package is a virtualized infinite scroller which enables the scrolling of unlimited items of unknown dimensions. Only items in the vicinity of the current viewport are rendered.
Works best for super large lists like an activity feed or photo gallery
npm install svelte-magic-scroller
yarn i svelte-magic-scroller
pnpm i svelte-magic-scroller
<script>
const INITIAL_LENGTH = 5000000000000;
let length = $state(INITIAL_LENGTH); // list size
let index = $state(0); // first full item visible in viewport
let offset = $state(0); // y offset from top
let ref = $state();
</script>
{#snippet item(i)}
<Item index={i} {length} />
{/snippet}
<MagicScroller
bind:this={ref}
bind:index
bind:offset
itemStyle={`display: flex; justify-content: center;`}
width="100%"
height="100%"
{length}
{item}
/>
Friction coefficients for different scroll speeds
Velocity thresholds
Bounce effect configuration
config = {
buffer: 15,
momentum: 0.8,
friction: {
fast: 0.99,
medium: 0.98,
slow: 0.95
},
velocity: {
fast: 30,
medium: 15
},
bounce: {
tension: 0.3,
returnSpeed: 0.15
}
}
You can pull from the base configuration if you only want to change a few values to keep the rest on the defaults.
config = {
...baseScrollerConfig,
buffer: 50
}
You can use goto(index)
to jump to any place in the list (e.g. ref?.goto(256000)
). If you would like to offset the location, you can add to the optional parameters (e.g. ref?.goto(256000, { offset: 64})
).
The goto
function does not support smooth scrolling when performing large list jumps, unless the target item is already in the buffer.
It is recommended to implement your own scroll bar to match your UX requirements. This is due to handling experience as the list size increase - the implementation of a scrollbar with 10,000 will likely need to differ significantly to another list with 1,000,000+ items.
A basic scrollbar example can be found in the repo here
index
and offset
are bindable and can be set to move to any location on the list, it is recommended to use goto()
to set the correct positions at the start/end of the list