- Docs
- Getting started
Getting started
Install @samline/is-visible with a package manager or a script tag and watch your first elements.
Install @samline/is-visible, then follow the Package / ESM or the HTML / CDN path to the first lazy image. Both use the same API.
Requirements
- Any modern browser. Older browsers without
IntersectionObserverget the fallback. - No runtime dependencies. React, Vue and Svelte are optional peers, only for their adapters.
Install
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>Package / ESM
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
},
})
The default export is the same isVisible function. From CommonJS use the named export: const { isVisible } = require('@samline/is-visible').
HTML / CDN
For Shopify, WordPress or plain HTML pages without a build step, load the browser bundle with a classic script tag. Pin the version in production.
<script src="https://cdn.jsdelivr.net/npm/@samline/is-visible@1.1.2/dist/browser/is-visible.global.js"></script>
<script>
window.isVisible('.card', {
once: true,
visible: (entry, card) => card.classList.add('is-visible'),
})
</script>
Working starter files
A complete page you can save and open: the live preview runs the real package, and its files load the published browser bundle.
Scroll the box to bring each section in. once stops observing after the first entry. Without JavaScript or IntersectionObserver the content stays visible.
Explore the code
HTML / CDN
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Reveal sections once as they enter</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div id="list" tabindex="0" role="region" aria-label="Scrollable sections">
<section class="reveal">First section</section>
<section class="reveal">Second section</section>
<section class="reveal">Third section</section>
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/is-visible@1.1.2/dist/browser/is-visible.global.js"></script>
<script src="./main.js" defer></script>
</body>
</html>
CSS
#list {
height: 14rem;
overflow-y: auto;
display: grid;
gap: 6rem;
padding: 1rem;
border: 1px solid #ccc;
}
.js .reveal {
opacity: 0;
transform: translateY(0.75rem);
transition:
opacity 0.6s,
transform 0.6s;
}
.js .reveal.is-visible {
opacity: 1;
transform: none;
}
@media (prefers-reduced-motion: reduce) {
.js .reveal {
transition: none;
}
}
JavaScript
document.documentElement.classList.add('js')
const list = document.querySelector('#list')
window.isVisible(list.querySelectorAll('.reveal'), {
root: list,
once: true,
threshold: 0.2,
visible: (entry, section) => section.classList.add('is-visible'),
})
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Reveal sections once as they enter</title>
<style>
#list {
height: 14rem;
overflow-y: auto;
display: grid;
gap: 6rem;
padding: 1rem;
border: 1px solid #ccc;
}
.js .reveal {
opacity: 0;
transform: translateY(0.75rem);
transition:
opacity 0.6s,
transform 0.6s;
}
.js .reveal.is-visible {
opacity: 1;
transform: none;
}
@media (prefers-reduced-motion: reduce) {
.js .reveal {
transition: none;
}
}
</style>
</head>
<body>
<main>
<div id="list" tabindex="0" role="region" aria-label="Scrollable sections">
<section class="reveal">First section</section>
<section class="reveal">Second section</section>
<section class="reveal">Third section</section>
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/is-visible@1.1.2/dist/browser/is-visible.global.js"></script>
<script>
document.documentElement.classList.add('js')
const list = document.querySelector('#list')
window.isVisible(list.querySelectorAll('.reveal'), {
root: list,
once: true,
threshold: 0.2,
visible: (entry, section) => section.classList.add('is-visible'),
})
</script>
</body>
</html>
Package / ESM
HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Reveal sections once as they enter</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div id="list" tabindex="0" role="region" aria-label="Scrollable sections">
<section class="reveal">First section</section>
<section class="reveal">Second section</section>
<section class="reveal">Third section</section>
</div>
</main>
<script type="module" src="./main.js"></script>
</body>
</html>
CSS
#list {
height: 14rem;
overflow-y: auto;
display: grid;
gap: 6rem;
padding: 1rem;
border: 1px solid #ccc;
}
.js .reveal {
opacity: 0;
transform: translateY(0.75rem);
transition:
opacity 0.6s,
transform 0.6s;
}
.js .reveal.is-visible {
opacity: 1;
transform: none;
}
@media (prefers-reduced-motion: reduce) {
.js .reveal {
transition: none;
}
}
JavaScript
import { isVisible } from '@samline/is-visible'
document.documentElement.classList.add('js')
const list = document.querySelector('#list')
isVisible(list.querySelectorAll('.reveal'), {
root: list,
once: true,
threshold: 0.2,
visible: (entry, section) => section.classList.add('is-visible'),
})
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Reveal sections once as they enter</title>
<style>
#list {
height: 14rem;
overflow-y: auto;
display: grid;
gap: 6rem;
padding: 1rem;
border: 1px solid #ccc;
}
.js .reveal {
opacity: 0;
transform: translateY(0.75rem);
transition:
opacity 0.6s,
transform 0.6s;
}
.js .reveal.is-visible {
opacity: 1;
transform: none;
}
@media (prefers-reduced-motion: reduce) {
.js .reveal {
transition: none;
}
}
</style>
</head>
<body>
<main>
<div id="list" tabindex="0" role="region" aria-label="Scrollable sections">
<section class="reveal">First section</section>
<section class="reveal">Second section</section>
<section class="reveal">Third section</section>
</div>
</main>
<script type="module">
import { isVisible } from '@samline/is-visible'
document.documentElement.classList.add('js')
const list = document.querySelector('#list')
isVisible(list.querySelectorAll('.reveal'), {
root: list,
once: true,
threshold: 0.2,
visible: (entry, section) => section.classList.add('is-visible'),
})
</script>
</body>
</html>
Next steps
Live previews with the complete, copyable source of each one.
API referenceSignatures and behavior of isVisible, observeVisibility and their callbacks.
Defaults and accepted values of inOut, once, root, rootMargin, threshold and fallback.
React, Vue and Svelte adapters.