Is Visible examples.
Live previews of distinct scenarios, each with independently copyable files. The same examples appear in the documentation.
Each preview runs the real package. The copied files load the published browser bundle, so they run as they are.
Reveal sections once as they enter
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>
Load content just before it is needed
Scroll the box: each story loads just before it arrives. rootMargin starts loading 200px before an item scrolls in. Each item loads exactly once.
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>Load content just before it is needed</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div id="feed" tabindex="0" role="region" aria-label="Scrollable feed">
<article data-src="Story 1">Loading…</article>
<article data-src="Story 2">Loading…</article>
<article data-src="Story 3">Loading…</article>
<article data-src="Story 4">Loading…</article>
<article data-src="Story 5">Loading…</article>
</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
#feed {
height: 14rem;
overflow-y: auto;
display: grid;
gap: 10rem;
padding: 1rem;
border: 1px solid #ccc;
}
JavaScript
const feed = document.querySelector('#feed')
window.isVisible(feed.querySelectorAll('[data-src]'), {
root: feed,
once: true,
rootMargin: '0px 0px 200px 0px',
visible: (entry, article) => {
article.textContent = `${article.dataset.src} loaded`
},
})
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Load content just before it is needed</title>
<style>
#feed {
height: 14rem;
overflow-y: auto;
display: grid;
gap: 10rem;
padding: 1rem;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<main>
<div id="feed" tabindex="0" role="region" aria-label="Scrollable feed">
<article data-src="Story 1">Loading…</article>
<article data-src="Story 2">Loading…</article>
<article data-src="Story 3">Loading…</article>
<article data-src="Story 4">Loading…</article>
<article data-src="Story 5">Loading…</article>
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/is-visible@1.1.2/dist/browser/is-visible.global.js"></script>
<script>
const feed = document.querySelector('#feed')
window.isVisible(feed.querySelectorAll('[data-src]'), {
root: feed,
once: true,
rootMargin: '0px 0px 200px 0px',
visible: (entry, article) => {
article.textContent = `${article.dataset.src} loaded`
},
})
</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>Load content just before it is needed</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div id="feed" tabindex="0" role="region" aria-label="Scrollable feed">
<article data-src="Story 1">Loading…</article>
<article data-src="Story 2">Loading…</article>
<article data-src="Story 3">Loading…</article>
<article data-src="Story 4">Loading…</article>
<article data-src="Story 5">Loading…</article>
</div>
</main>
<script type="module" src="./main.js"></script>
</body>
</html>
CSS
#feed {
height: 14rem;
overflow-y: auto;
display: grid;
gap: 10rem;
padding: 1rem;
border: 1px solid #ccc;
}
JavaScript
import { isVisible } from '@samline/is-visible'
const feed = document.querySelector('#feed')
isVisible(feed.querySelectorAll('[data-src]'), {
root: feed,
once: true,
rootMargin: '0px 0px 200px 0px',
visible: (entry, article) => {
article.textContent = `${article.dataset.src} loaded`
},
})
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Load content just before it is needed</title>
<style>
#feed {
height: 14rem;
overflow-y: auto;
display: grid;
gap: 10rem;
padding: 1rem;
border: 1px solid #ccc;
}
</style>
</head>
<body>
<main>
<div id="feed" tabindex="0" role="region" aria-label="Scrollable feed">
<article data-src="Story 1">Loading…</article>
<article data-src="Story 2">Loading…</article>
<article data-src="Story 3">Loading…</article>
<article data-src="Story 4">Loading…</article>
<article data-src="Story 5">Loading…</article>
</div>
</main>
<script type="module">
import { isVisible } from '@samline/is-visible'
const feed = document.querySelector('#feed')
isVisible(feed.querySelectorAll('[data-src]'), {
root: feed,
once: true,
rootMargin: '0px 0px 200px 0px',
visible: (entry, article) => {
article.textContent = `${article.dataset.src} loaded`
},
})
</script>
</body>
</html>
Run work only while it is on screen
Scroll the box down to the panel and back: the count runs only while it is visible. inOut reports leaving too: a counter runs while half of its panel is visible and pauses when it leaves.
Scroll down to the panel.
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>Run work only while it is on screen</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div id="box" tabindex="0" role="region" aria-label="Scrollable box">
<p>Scroll down to the panel.</p>
<div id="panel">Running: <output id="ticks" aria-live="off">0</output></div>
<p style="height: 20rem"></p>
</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
#box {
height: 12rem;
overflow-y: auto;
padding: 1rem;
border: 1px solid #ccc;
}
#panel {
margin-top: 14rem;
padding: 1rem;
border-left: 4px solid #15803d;
background: #f3f3f3;
}
JavaScript
const box = document.querySelector('#box')
const panel = document.querySelector('#panel')
const ticks = document.querySelector('#ticks')
let timer = null
window.isVisible(panel, {
root: box,
inOut: true,
threshold: 0.5,
visible: () => {
timer = setInterval(() => (ticks.textContent = String(Number(ticks.textContent) + 1)), 200)
},
notVisible: () => clearInterval(timer),
})
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Run work only while it is on screen</title>
<style>
#box {
height: 12rem;
overflow-y: auto;
padding: 1rem;
border: 1px solid #ccc;
}
#panel {
margin-top: 14rem;
padding: 1rem;
border-left: 4px solid #15803d;
background: #f3f3f3;
}
</style>
</head>
<body>
<main>
<div id="box" tabindex="0" role="region" aria-label="Scrollable box">
<p>Scroll down to the panel.</p>
<div id="panel">Running: <output id="ticks" aria-live="off">0</output></div>
<p style="height: 20rem"></p>
</div>
</main>
<script src="https://cdn.jsdelivr.net/npm/@samline/is-visible@1.1.2/dist/browser/is-visible.global.js"></script>
<script>
const box = document.querySelector('#box')
const panel = document.querySelector('#panel')
const ticks = document.querySelector('#ticks')
let timer = null
window.isVisible(panel, {
root: box,
inOut: true,
threshold: 0.5,
visible: () => {
timer = setInterval(
() => (ticks.textContent = String(Number(ticks.textContent) + 1)),
200
)
},
notVisible: () => clearInterval(timer),
})
</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>Run work only while it is on screen</title>
<link rel="stylesheet" href="./style.css" />
</head>
<body>
<main>
<div id="box" tabindex="0" role="region" aria-label="Scrollable box">
<p>Scroll down to the panel.</p>
<div id="panel">Running: <output id="ticks" aria-live="off">0</output></div>
<p style="height: 20rem"></p>
</div>
</main>
<script type="module" src="./main.js"></script>
</body>
</html>
CSS
#box {
height: 12rem;
overflow-y: auto;
padding: 1rem;
border: 1px solid #ccc;
}
#panel {
margin-top: 14rem;
padding: 1rem;
border-left: 4px solid #15803d;
background: #f3f3f3;
}
JavaScript
import { isVisible } from '@samline/is-visible'
const box = document.querySelector('#box')
const panel = document.querySelector('#panel')
const ticks = document.querySelector('#ticks')
let timer = null
isVisible(panel, {
root: box,
inOut: true,
threshold: 0.5,
visible: () => {
timer = setInterval(() => (ticks.textContent = String(Number(ticks.textContent) + 1)), 200)
},
notVisible: () => clearInterval(timer),
})
Complete file
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Run work only while it is on screen</title>
<style>
#box {
height: 12rem;
overflow-y: auto;
padding: 1rem;
border: 1px solid #ccc;
}
#panel {
margin-top: 14rem;
padding: 1rem;
border-left: 4px solid #15803d;
background: #f3f3f3;
}
</style>
</head>
<body>
<main>
<div id="box" tabindex="0" role="region" aria-label="Scrollable box">
<p>Scroll down to the panel.</p>
<div id="panel">Running: <output id="ticks" aria-live="off">0</output></div>
<p style="height: 20rem"></p>
</div>
</main>
<script type="module">
import { isVisible } from '@samline/is-visible'
const box = document.querySelector('#box')
const panel = document.querySelector('#panel')
const ticks = document.querySelector('#ticks')
let timer = null
isVisible(panel, {
root: box,
inOut: true,
threshold: 0.5,
visible: () => {
timer = setInterval(
() => (ticks.textContent = String(Number(ticks.textContent) + 1)),
200
)
},
notVisible: () => clearInterval(timer),
})
</script>
</body>
</html>
These examples show a few possibilities. The next ideas are yours: combine the package’s capabilities with your own content, design and interactions to create the experience your project needs.
Read the example guideDownload the Is Visible skill for AI agents