Installations
npm install svelte-intersection-observer
Developer Guide
Typescript
Yes
Module System
ESM
Node Version
18.19.0
NPM Version
10.2.3
Score
77.9
Supply Chain
100
Quality
77.6
Maintenance
100
Vulnerability
100
License
Releases
Contributors
Unable to fetch Contributors
Languages
Svelte (54.39%)
TypeScript (42.1%)
HTML (2.38%)
JavaScript (1.12%)
Developer
Download Statistics
Total Downloads
400,369
Last Day
400
Last Week
3,388
Last Month
15,656
Last Year
147,955
GitHub Statistics
327 Stars
176 Commits
8 Forks
4 Watching
2 Branches
5 Contributors
Bundle Size
2.00 kB
Minified
0.98 kB
Minified + Gzipped
Package Meta Information
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
Total Downloads
Cumulative downloads
Total Downloads
400,369
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
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
No dependencies detected.
svelte-intersection-observer
Detect if an element is in the viewport using the Intersection Observer API.
Try it in the Svelte REPL.
Installation
Yarn
1yarn add -D svelte-intersection-observer
NPM
1npm i -D svelte-intersection-observer
pnpm
1pnpm i -D svelte-intersection-observer
Usage
Basic
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>
Once
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
event
The 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
event
As 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>
API
Props
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 |
Dispatched events
- on:observe: fired when the element is first observed or whenever an intersection change occurs
- on:intersect: fired when the element is intersecting the viewport
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}
Changelog
License
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
- Info: project has a license file: LICENSE:0
- Info: FSF or OSI recognized license: MIT License: LICENSE:0
Reason
packaging workflow detected
Details
- Info: Project packages its releases by way of GitHub Actions.: .github/workflows/release.yml:7
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
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:10: update your workflow using https://app.stepsecurity.io/secureworkflow/metonym/svelte-intersection-observer/ci.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/ci.yml:11: update your workflow using https://app.stepsecurity.io/secureworkflow/metonym/svelte-intersection-observer/ci.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/ci.yml:12: update your workflow using https://app.stepsecurity.io/secureworkflow/metonym/svelte-intersection-observer/ci.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/ci.yml:37: update your workflow using https://app.stepsecurity.io/secureworkflow/metonym/svelte-intersection-observer/ci.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:13: update your workflow using https://app.stepsecurity.io/secureworkflow/metonym/svelte-intersection-observer/release.yml/master?enable=pin
- Warn: third-party GitHubAction not pinned by hash: .github/workflows/release.yml:14: update your workflow using https://app.stepsecurity.io/secureworkflow/metonym/svelte-intersection-observer/release.yml/master?enable=pin
- Warn: GitHub-owned GitHubAction not pinned by hash: .github/workflows/release.yml:15: update your workflow using https://app.stepsecurity.io/secureworkflow/metonym/svelte-intersection-observer/release.yml/master?enable=pin
- Info: 0 out of 4 GitHub-owned GitHubAction dependencies pinned
- Info: 0 out of 3 third-party GitHubAction dependencies pinned
Reason
detected GitHub workflow tokens with excessive permissions
Details
- Info: jobLevel 'contents' permission set to 'read': .github/workflows/release.yml:10
- Warn: no topLevel permission defined: .github/workflows/ci.yml:1
- Warn: no topLevel permission defined: .github/workflows/release.yml:1
- Info: no jobLevel write permissions found
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 18 are checked with a SAST tool
Score
3.8
/10
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 MoreOther packages similar to svelte-intersection-observer
svelte-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.