svelte-inview
A Svelte action that monitors when an element enters or leaves the viewport/parent element. It leverages the [Intersection Observer API](https://developer.mozilla.org/en-US/docs/Web/API/Intersection_Observer_API ) for performance and efficiency.
Key Features:
- šļø Tracks element visibility in viewport or parent element
- šļø Blazing fast with Intersection Observer (non-blocking)
- š¦ļø Lightweight (~2kb) with no external dependencies (except Svelte)
- šļø Versatile use cases: lazy loading, infinite scrolling, video control, animations, etc.
- š„ Simple API with easy integration
- āļø Detects scrolling direction (vertical/horizontal)
Target Audience:
Svelte developers needing viewport visibility detection for various UI interactions and optimizations.
Core Functionality:
- Basic element visibility tracking
- Lazy loading images with
rootMargin
andunobserveOnEnter
- Video play/pause control when in/out of viewport
- Scroll-triggered animations with direction detection
Event System:
inview_change
: Fires when visibility changesinview_enter
: When element enters viewportinview_leave
: When element leaves viewportinview_init
: On action initialization
Configuration Options:
root
: Viewport element (default: window)rootMargin
: Detection margin around rootthreshold
: Visibility percentage trigger pointsunobserveOnEnter
: Stop observing after first appearance
Output Data:
- Visibility state (
inView
) - IntersectionObserverEntry details
- Scroll direction (vertical/horizontal)
- Observer instance and target node
Demo:
Live example available at [svelte-inview.netlify.app](https://svelte-inview.netlify.app/ )
hacktoberfestsvelteobserverintersectionobserverintersection-observerjavascriptintersectionobserver-apisvelte3svelte-jssvelte-componentviewporttypescript
Author
Featured Stories
No featured articles here. Navigate to an article, then pick the Feature on menu button