See it. React to it.

Is Visible
Know what is on screen.

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

About 1 KB gzipZero dependenciesMIT licensed

Is Visible previewLive preview

Scroll the box. Each card reports when half of it is visible.

Alphahidden
Bravohidden
Charliehidden
Deltahidden
Echohidden
Foxtrothidden

0 of 6 visible

ONE SMALL SCRIPT. RIGHT AT HOME IN YOUR STACK.

JavaScript TypeScript React Vue Svelte Astro Shopify WordPress
01 / More than an observer

One call per idea.
Not one per element.

Any target

Pass an element, a selector, a NodeList or an array. One cleanup stops them all.

Content never stuck hidden

Without IntersectionObserver every element is reported visible once, so reveal effects degrade to visible content.

Honest transitions

visible fires once per entry and notVisible only after the element was visible: no false "left" events on load.

Shared observers

Calls with the same root, rootMargin and threshold share one IntersectionObserver that disconnects when unused.

Idiomatic adapters

useIsVisible for React and Vue, an action and stores for Svelte, and a VisibilityObserver component.

Safe on the server

No DOM access at import time. Without a DOM every call returns a no-op cleanup.

02 / Fit it into your stack

One package. Every runtime.

The root entry covers vanilla JavaScript; adapters are subpaths.

Package

@samline/is-visible

isVisible and observeVisibility as ESM or CommonJS.

React

@samline/is-visible/react

useIsVisible and the VisibilityObserver component.

Vue

@samline/is-visible/vue

useIsVisible and the VisibilityObserver component.

HTML / CDN

window.isVisible

A classic script for pages without a build step.

Svelte

@samline/is-visible/svelte

useIsVisible stores and createVisibilityAction.

Types

IsVisibleOptions

Typed options and callbacks, no CSS required.

03 / Install and watch

One install. Lazy images.

Load each image when it is 200px from entering the viewport.

Read the installation guide
1

Install

npm

Terminal
npm install @samline/is-visible

pnpm

Terminal
pnpm add @samline/is-visible

yarn

Terminal
yarn add @samline/is-visible

bun

Terminal
bun add @samline/is-visible

HTML / CDN

Terminal
<script src="https://cdn.jsdelivr.net/npm/@samline/is-visible@1.1.2/dist/browser/is-visible.global.js" defer></script>
2

Add markup

Keep the real URL in data-src. Width and height avoid layout shifts.

index.html
<img data-src="photo.jpg" alt="A mountain lake" width="640" height="400" />
3

Watch the images

main.js
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
  },
})
Copy complete working files

Continue with roots, thresholds and the fallback behaviour.

Explore the options
04 / Beyond lazy images

Same call, new jobs.
Pause, scope, clean up.

Three common jobs built on the same call.

inOut

Pause media off-screen

Play a video while half of it is visible and pause it when it leaves.

Read the options
main.js
isVisible(video, {
  inOut: true,
  threshold: 0.5,
  visible: () => video.play(),
  notVisible: () => video.pause(),
})
root

Observe inside a scroll box

Track items against their own scroll container instead of the viewport.

Read the options
main.js
isVisible(list.children, { root: list, visible: (entry, item) => item.classList.add('seen') })
cleanup

Clean up on teardown

Every call returns one function that stops observing all its elements.

Read the API
main.js
const stop = isVisible('.card', { visible })
// later
stop()
Explore working examples
05 / Before you build

A few details. Clearly answered.

Why does notVisible not fire on page load?
Since 1.0, notVisible only fires after an element was visible. An element that starts off-screen is simply not visible yet.
Does it work without a bundler?
Yes. Load dist/browser/is-visible.global.js with a classic script tag and call window.isVisible. Pin the version in production.
What changed in 1.0?
Shared observers, selector and list targets, (entry, element) callbacks, top-level root, rootMargin and threshold, and a fallback for old browsers. The changelog lists every change.
Can I observe elements inside a scroll container?
Yes. Pass the container as root; rootMargin and threshold apply to it instead of the viewport.
How do I fire before an element scrolls in?
Use a positive bottom rootMargin, for example '0px 0px 200px 0px', to start loading 200px early.
Is there a React hook?
Yes. useIsVisible from @samline/is-visible/react returns { ref, isVisible, entry }; attach ref to the element. VisibilityObserver wraps it as a component.
Does it create one observer per element?
No. Calls with the same root, rootMargin and threshold share one IntersectionObserver, which disconnects when it watches nothing.
What happens in browsers without IntersectionObserver?
With the default fallback every element is reported visible once, so content revealed by your code is never left hidden. Use fallback: 'none' to do nothing instead.
Only what is on screen

Do the work when it is seen.

Try the examples or follow the first steps.

Guides, the complete API and release notes, in one place.

Read the documentation