All Projects → metonym → svelte-intersection-observer

metonym / svelte-intersection-observer

Licence: MIT License
Detect if an element is in the viewport using the Intersection Observer API

Programming Languages

Svelte
593 projects
javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to svelte-intersection-observer

svelte-inview
A Svelte action that monitors an element enters or leaves the viewport.🔥
Stars: ✭ 358 (+137.09%)
Mutual labels:  svelte, viewport, intersection-observer, svelte-component
React Intersection Observer
React implementation of the Intersection Observer API to tell you when an element enters or leaves the viewport.
Stars: ✭ 2,689 (+1680.79%)
Mutual labels:  viewport, lazy-loading, intersection-observer
sveld
Generate TypeScript definitions for your Svelte components
Stars: ✭ 281 (+86.09%)
Mutual labels:  svelte, typescript-definitions, svelte-component
vue-observable
IntersectionObserver, MutationObserver and PerformanceObserver in Vue.js
Stars: ✭ 24 (-84.11%)
Mutual labels:  viewport, intersection-observer
Ng In Viewport
Allows us to check if an element is within the browsers visual viewport
Stars: ✭ 178 (+17.88%)
Mutual labels:  viewport, lazy-loading
svelte-search
Accessible, customizable Svelte search component
Stars: ✭ 17 (-88.74%)
Mutual labels:  svelte, svelte-component
React On Screen
Check if a react component in the viewport
Stars: ✭ 357 (+136.42%)
Mutual labels:  viewport, lazy-loading
kahi-ui
Straight-forward Svelte UI for the Web
Stars: ✭ 169 (+11.92%)
Mutual labels:  svelte, typescript-definitions
svelte-lottie-player
Lottie Player component for Svelte
Stars: ✭ 90 (-40.4%)
Mutual labels:  svelte, svelte-component
svelte-marquee-text
[CSS GPU Animation] Marquee Text for Svelte
Stars: ✭ 47 (-68.87%)
Mutual labels:  svelte, svelte-component
svelte-undoable
Memento design pattern in Svelte
Stars: ✭ 39 (-74.17%)
Mutual labels:  svelte, svelte-component
svelte-multiselect
Keyboard-friendly, accessible and highly customizable multi-select component
Stars: ✭ 91 (-39.74%)
Mutual labels:  svelte, svelte-component
carbon-components-svelte
Svelte implementation of the Carbon Design System
Stars: ✭ 1,615 (+969.54%)
Mutual labels:  svelte, typescript-definitions
Viewprt
A tiny, dependency-free, high performance viewport position & intersection observation tool
Stars: ✭ 36 (-76.16%)
Mutual labels:  viewport, lazy-loading
Skeleton Elements
Skeleton elements - UI for improved perceived performance
Stars: ✭ 27 (-82.12%)
Mutual labels:  svelte, lazy-loading
React Cool Inview
😎 🖥️ React hook to monitor an element enters or leaves the viewport (or another element).
Stars: ✭ 830 (+449.67%)
Mutual labels:  viewport, lazy-loading
vue-in-viewport-mixin
Vue 2 mixin to determine when a DOM element is visible in the client window
Stars: ✭ 99 (-34.44%)
Mutual labels:  viewport, intersection-observer
react-spring-pop
Animate React elements when they enter the viewport with physics based animations
Stars: ✭ 17 (-88.74%)
Mutual labels:  viewport, intersection-observer
react-awesome-reveal
React components to add reveal animations using the Intersection Observer API and CSS Animations.
Stars: ✭ 564 (+273.51%)
Mutual labels:  viewport, intersection-observer
EddieHubCommunity.github.io
Information about our community
Stars: ✭ 88 (-41.72%)
Mutual labels:  svelte

svelte-intersection-observer

NPM

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

Try it in the Svelte REPL.

Installation

Yarn

yarn add -D svelte-intersection-observer

NPM

npm i -D svelte-intersection-observer

pnpm

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

<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 first intersection event occurs.

<script>
  import IntersectionObserver from "svelte-intersection-observer";

  let elementOnce;
  let intersectOnce;
</script>

<header class:intersecting={intersectOnce}>
  {intersectOnce ? "Element is in view" : "Element is not in view"}
</header>

<IntersectionObserver
  once
  element={elementOnce}
  bind:intersecting={intersectOnce}
>
  <div bind:this={elementOnce}>Hello world</div>
</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.

<script>
  import IntersectionObserver from "svelte-intersection-observer";
  import { fade } from "svelte/transition";

  let node;
</script>

<header />

<IntersectionObserver element={node} let:intersecting>
  <div bind:this={node}>
    {#if intersecting}
      <div transition:fade={{ delay: 200 }}>Hello world</div>
    {/if}
  </div>
</IntersectionObserver>

on:observe event

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>

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.

<IntersectionObserver
  {element}
  on:intersect={(e) => {
    console.log(e.detail); // IntersectionObserverEntry
    console.log(e.detail.isIntersecting); // true
  }}
>
  <div bind:this={element}>Hello world</div>
</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 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
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;
  };
}

Examples

The examples folder contains sample set-ups.

TypeScript support

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.

Changelog

Changelog

License

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].