Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

svelte-intersection-observer

Package Overview
Dependencies
Maintainers
1
Versions
18
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

svelte-intersection-observer

Detect if an element is in the viewport using the Intersection Observer API

  • 0.7.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
3.2K
increased by41.03%
Maintainers
1
Weekly downloads
 
Created
Source

svelte-intersection-observer

NPM

Detect if an element is in the viewport using the Intersection Observer API.

Try it in the Svelte REPL.

Install

yarn add -D svelte-intersection-observer
# OR
npm i -D svelte-intersection-observer

Usage

Basic

<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>

Once

Set once to true for the intersection event to occur only once.

The element will be unobserved after the intersection 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>

on:intersect event

The "intersect" event is dispatched only if the observed element is intersecting the viewport.

<IntersectionObserver
  {element}
  on:intersect="{(e) => {
    console.log(e.detail); // IntersectionObserverEntry
  }}"
>
  <div bind:this="{element}">Hello world</div>
</IntersectionObserver>

API

Props

Prop nameDescriptionValue
elementElement observed for intersectionHTMLElement
rootContaining elementnull or HTMLElement (default: null)
rootMarginMargin offset of the containing elementstring (default: "0px")
thresholdPercentage of element visibility to trigger an eventnumber between 0 and 1 (default: 0)
entryObserved element metadataIntersectionObserverEntry
onceIf true, the observed element will be unobserved upon intersectboolean (default: false)
intersectingtrue if the observed element is intersecting the viewportboolean (default: false)
observerIntersectionObserver instanceIntersectionObserver

Dispatched events

  • on:observe: fired when an intersection change occurs (type IntersectionObserverEntry)
  • on:intersect: fired when an intersection change occurs and the element is intersecting (type IntersectionObserverEntry)

TypeScript support

Svelte version 3.31.0 or greater is required to use this module with TypeScript.

Changelog

Changelog

License

MIT

Keywords

FAQs

Package last updated on 23 Apr 2021

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc