0.8.0 • Published 2 years ago
@fuz.dev/svelte_intersect v0.8.0
@fuz.dev/svelte_intersect
- demo: intersect.fuz.dev
- npm:
@fuz.dev/svelte_intersect
- Svelte repl
- todo
- figure out how to make the
el
type inferred/generic
- figure out how to make the
Usage
npm i -D @fuz.dev/svelte_intersect
import {intersect} from '@fuz.dev/svelte_intersect';
intersect
is a Svelte action
that calls your callback when el
enters or leaves the viewport:
<div use:intersect={(intersecting, el, disconnect) => /* ... */}>
export interface Intersect_Callback {
(intersecting: boolean, el: HTMLElement | SVGElement, disconnect: () => void): void;
}
For more complex behavior:
<div use:intersect={{
cb: (intersecting, el, disconnect) => /* ... */,
count: 1, // 1 is like 'once', 0 disables, <0 infinite
options: {threshold, root, rootMagin}, // IntersectionObserver options
}}>
property | default | description |
---|---|---|
cb | called every time the element enters or leaves the viewport until the action's observer is disconnected | |
count? | undefined | pass 1 to disconnect after el enters and leaves the viewport one time, similar to 'once' for an event - disable the callback with 0 , and never disconnect with undefined or a negative number like -1 |
options? | undefined | IntersectionObserverInit options forwarded to the IntersectionObserver |
For more see the IntersectionObserver docs on MDN and the demo at intersect.fuz.dev.