Gathering detailed insights and metrics for svelte-intersection-observer
Gathering detailed insights and metrics for svelte-intersection-observer
Gathering detailed insights and metrics for svelte-intersection-observer
Gathering detailed insights and metrics for svelte-intersection-observer
npm install svelte-intersection-observer
Typescript
Module System
Node Version
NPM Version
77.9
Supply Chain
100
Quality
77.6
Maintenance
100
Vulnerability
100
License
Svelte (54.39%)
TypeScript (42.1%)
HTML (2.38%)
JavaScript (1.12%)
Total Downloads
400,369
Last Day
400
Last Week
3,388
Last Month
15,656
Last Year
147,955
327 Stars
176 Commits
8 Forks
4 Watching
2 Branches
5 Contributors
Minified
Minified + Gzipped
Latest Version
1.0.0
Package Id
svelte-intersection-observer@1.0.0
Unpacked Size
12.96 kB
Size
3.79 kB
File Count
7
NPM Version
10.2.3
Node Version
18.19.0
Publised On
01 Jan 2024
Cumulative downloads
Total Downloads
Last day
-57.9%
400
Compared to previous day
Last week
-5.6%
3,388
Compared to previous week
Last month
8.7%
15,656
Compared to previous month
Last year
30.4%
147,955
Compared to previous year
No dependencies detected.
Detect if an element is in the viewport using the Intersection Observer API.
Try it in the Svelte REPL.
Yarn
1yarn add -D svelte-intersection-observer
NPM
1npm i -D svelte-intersection-observer
pnpm
1pnpm i -D svelte-intersection-observer
Use the bind:this
directive to pass an element reference to the IntersectionObserver
component.
Then, simply bind to the reactive intersecting
prop to determine if the element intersects the viewport.
1<script> 2 import IntersectionObserver from "svelte-intersection-observer"; 3 4 let element; 5 let intersecting; 6</script> 7 8<header class:intersecting> 9 {intersecting ? "Element is in view" : "Element is not in view"} 10</header> 11 12<IntersectionObserver {element} bind:intersecting> 13 <div bind:this={element}>Hello world</div> 14</IntersectionObserver>
Set once
to true
for the intersection event to occur only once. The element
will be unobserved after the first intersection event occurs.
1<script> 2 import IntersectionObserver from "svelte-intersection-observer"; 3 4 let elementOnce; 5 let intersectOnce; 6</script> 7 8<header class:intersecting={intersectOnce}> 9 {intersectOnce ? "Element is in view" : "Element is not in view"} 10</header> 11 12<IntersectionObserver 13 once 14 element={elementOnce} 15 bind:intersecting={intersectOnce} 16> 17 <div bind:this={elementOnce}>Hello world</div> 18</IntersectionObserver>
let:intersecting
An alternative to binding to the intersecting
prop is to use the let:
directive.
In the following example, the "Hello world" element will fade in when its containing element intersects the viewport.
1<script> 2 import IntersectionObserver from "svelte-intersection-observer"; 3 import { fade } from "svelte/transition"; 4 5 let node; 6</script> 7 8<header /> 9 10<IntersectionObserver element={node} let:intersecting> 11 <div bind:this={node}> 12 {#if intersecting} 13 <div transition:fade={{ delay: 200 }}>Hello world</div> 14 {/if} 15 </div> 16</IntersectionObserver>
on:observe
eventThe observe
event is dispatched when the element is first observed and also whenever an intersection event occurs.
1<IntersectionObserver 2 {element} 3 on:observe={(e) => { 4 console.log(e.detail); // IntersectionObserverEntry 5 console.log(e.detail.isIntersecting); // true | false 6 }} 7> 8 <div bind:this={element}>Hello world</div> 9</IntersectionObserver>
on:intersect
eventAs an alternative to binding the intersecting
prop, you can listen to the intersect
event that is dispatched if the observed element is intersecting the viewport.
Note: Compared to on:observe
, on:intersect
is dispatched only when the element is intersecting the viewport. In other words, e.detail.isIntersecting
will only be true
.
1<IntersectionObserver 2 {element} 3 on:intersect={(e) => { 4 console.log(e.detail); // IntersectionObserverEntry 5 console.log(e.detail.isIntersecting); // true 6 }} 7> 8 <div bind:this={element}>Hello world</div> 9</IntersectionObserver>
Name | Description | Type | Default value |
---|---|---|---|
element | Observed element | HTMLElement | null |
once | Unobserve the element after the first intersection event | boolean | false |
intersecting | true if the observed element is intersecting the viewport | boolean | false |
root | Containing element | null or HTMLElement | null |
rootMargin | Margin offset of the containing element | string | "0px" |
threshold | Percentage of element visibile to trigger an event | number between 0 and 1, or an array of number s between 0 and 1 | 0 |
entry | Observed element metadata | IntersectionObserverEntry | null |
observer | IntersectionObserver instance | IntersectionObserver | null |
The e.detail
dispatched by the observe
and intersect
events is an IntersectionObserverEntry
interface.
Note that all properties in IntersectionObserverEntry
are read-only.
IntersectionObserverEntry
1interface IntersectionObserverEntry { 2 target: HTMLElement; 3 time: number; 4 isIntersecting: boolean; 5 isVisible: boolean; 6 intersectionRatio: number; 7 intersectionRect: { 8 bottom: number; 9 height: number; 10 left: number; 11 right: number; 12 top: number; 13 width: number; 14 x: number; 15 y: number; 16 }; 17 rootBounds: { 18 bottom: number; 19 height: number; 20 left: number; 21 right: number; 22 top: number; 23 width: number; 24 x: number; 25 y: number; 26 }; 27 boundingClientRect: { 28 bottom: number; 29 height: number; 30 left: number; 31 right: number; 32 top: number; 33 width: number; 34 x: number; 35 y: number; 36 }; 37}
No vulnerabilities found.
Reason
no dangerous workflow patterns detected
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
packaging workflow detected
Details
Reason
Found 1/22 approved changesets -- score normalized to 0
Reason
0 commit(s) and 1 issue activity found in the last 90 days -- score normalized to 0
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-12-16
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn Moresvelte-intersection-observer-action
Svelte use:action for element position notifications using IntersectionObserver.
svelte-inview
A Svelte action that monitors an element enters or leaves the viewport or a parent element. Performant and efficient thanks to using Intersection Observer under the hood.
svelte-action-intersection-observer
for svelte
svelte-intersection-tracker
This component uses IntersectionObserver to not only track if elements are intersecting but also monitors and compares the percentage they are visible. The elements with highest visiblity get flagged current.