Security News
The Risks of Misguided Research in Supply Chain Security
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
svelte-intersection-observer
Advanced tools
Detect if an element is in the viewport using the Intersection Observer API
Detect if an element is in the viewport using the Intersection Observer API.
Try it in the Svelte REPL.
Yarn
yarn add -D svelte-intersection-observer
NPM
npm 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.
<script>
import IntersectionObserver from "svelte-intersection-observer";
let element;
let intersecting;
</script>
<header class:intersecting>
{intersecting ? "Element is in view" : "Element is not in view"}
</header>
<IntersectionObserver {element} bind:intersecting>
<div bind:this={element}>Hello world</div>
</IntersectionObserver>
Set once
to true
for the intersection event to occur only once. The element
will be unobserved after the first intersection event occurs.
<script>
let element2;
let intersectOnce;
</script>
<header class:intersecting={intersectOnce}>
{intersectOnce ? "Element is in view" : "Element is not in view"}
</header>
<IntersectionObserver once element={element2} bind:intersecting={intersectOnce}>
<div bind:this={element2}>Hello world</div>
</IntersectionObserver>
The observe
event is dispatched when the element is first observed and also whenever an intersection event occurs.
<IntersectionObserver
{element}
on:observe="{(e) => {
console.log(e.detail); // IntersectionObserverEntry
console.log(e.detail.isIntersecting); // true | false
}}"
>
<div bind:this="{element}">Hello world</div>
</IntersectionObserver>
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
.
<IntersectionObserver
{element}
on:intersect="{(e) => {
console.log(e.detail); // IntersectionObserverEntry
console.log(e.detail.isIntersecting); // true
}}"
>
<div bind:this="{element}">Hello world</div>
</IntersectionObserver>
Name | Description | Type | Default value |
---|---|---|---|
element | Element observed for intersection | HTMLElement | null |
once | If true , the observed element will be unobserved upon intersection | 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 visibility to trigger an event | number 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
interface IntersectionObserverEntry {
target: HTMLElement;
time: number;
isIntersecting: boolean;
isVisible: boolean;
intersectionRatio: number;
intersectionRect: {
bottom: number;
height: number;
left: number;
right: number;
top: number;
width: number;
x: number;
y: number;
};
rootBounds: {
bottom: number;
height: number;
left: number;
right: number;
top: number;
width: number;
x: number;
y: number;
};
boundingClientRect: {
bottom: number;
height: number;
left: number;
right: number;
top: number;
width: number;
x: number;
y: number;
};
}
The examples folder contains sample set-ups.
Svelte version 3.31 or greater is required to use this module with TypeScript.
TypeScript definitions for this component are located in the types folder.
0.8.0 - 2021-09-02
.svelte.d.ts
extension for component TypeScript definitionFAQs
Detect if an element is in the viewport using the Intersection Observer API
The npm package svelte-intersection-observer receives a total of 3,176 weekly downloads. As such, svelte-intersection-observer popularity was classified as popular.
We found that svelte-intersection-observer demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Snyk's use of malicious npm packages for research raises ethical concerns, highlighting risks in public deployment, data exfiltration, and unauthorized testing.
Research
Security News
Socket researchers found several malicious npm packages typosquatting Chalk and Chokidar, targeting Node.js developers with kill switches and data theft.
Security News
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.