Any target
Pass an element, a selector, a NodeList or an array. One cleanup stops them all.
Run code when elements enter or leave the viewport, for one element or a whole selector, on observers shared across your page.
Install it, scroll the live preview, then read the full reference.
npm install @samline/is-visibleAbout 1 KB gzipZero dependenciesMIT licensed
Scroll the box. Each card reports when half of it is visible.
0 of 6 visible
ONE SMALL SCRIPT. RIGHT AT HOME IN YOUR STACK.
Pass an element, a selector, a NodeList or an array. One cleanup stops them all.
Without IntersectionObserver every element is reported visible once, so reveal effects degrade to visible content.
visible fires once per entry and notVisible only after the element was visible: no false "left" events on load.
Calls with the same root, rootMargin and threshold share one IntersectionObserver that disconnects when unused.
useIsVisible for React and Vue, an action and stores for Svelte, and a VisibilityObserver component.
No DOM access at import time. Without a DOM every call returns a no-op cleanup.
The root entry covers vanilla JavaScript; adapters are subpaths.
Package
isVisible and observeVisibility as ESM or CommonJS.
React
useIsVisible and the VisibilityObserver component.
Vue
useIsVisible and the VisibilityObserver component.
HTML / CDN
A classic script for pages without a build step.
Svelte
useIsVisible stores and createVisibilityAction.
Types
Typed options and callbacks, no CSS required.
Load each image when it is 200px from entering the viewport.
Read the installation guideInstall
npm
npm install @samline/is-visiblepnpm
pnpm add @samline/is-visibleyarn
yarn add @samline/is-visiblebun
bun add @samline/is-visibleHTML / CDN
<script src="https://cdn.jsdelivr.net/npm/@samline/is-visible@1.1.2/dist/browser/is-visible.global.js" defer></script>Add markup
Keep the real URL in data-src. Width and height avoid layout shifts.
<img data-src="photo.jpg" alt="A mountain lake" width="640" height="400" />Watch the images
import { isVisible } from '@samline/is-visible'
isVisible('img[data-src]', {
once: true,
rootMargin: '0px 0px 200px 0px',
visible: (entry, image) => {
image.src = image.dataset.src
},
})Continue with roots, thresholds and the fallback behaviour.
Explore the optionsThree common jobs built on the same call.
Play a video while half of it is visible and pause it when it leaves.
Read the optionsisVisible(video, {
inOut: true,
threshold: 0.5,
visible: () => video.play(),
notVisible: () => video.pause(),
})Track items against their own scroll container instead of the viewport.
Read the optionsisVisible(list.children, { root: list, visible: (entry, item) => item.classList.add('seen') })Every call returns one function that stops observing all its elements.
Read the APIconst stop = isVisible('.card', { visible })
// later
stop()Try the examples or follow the first steps.
Guides, the complete API and release notes, in one place.
Read the documentation