Skip to content
@samline/is-visiblev1.1.2
AboutInstallRecipesFAQExamplesDocs

@samline/is-visible

Explore Is Visible

A starting point, a working example, and the details when you need them.

  • 01AboutUnderstand the problem and the approach.
  • 02InstallChoose your setup and follow the first steps.
  • 03RecipesUseful patterns and integration context.
  • 04FAQFind the practical details.
  • 05ExamplesTry a preview and explore its code.
  • 06DocsRead guides and the complete reference.

Around the project

  • GitHub(opens in a new tab)
  • npm(opens in a new tab)

v1.1.2·MIT

GitHub(opens in a new tab)Get started
Examples

Is Visible examples.

Live previews of distinct scenarios, each with independently copyable files. The same examples appear in the documentation.

Try the exampleInstallation guide

Each preview runs the real package. The copied files load the published browser bundle, so they run as they are.

Choose an example

RevealLazy loadPause off-screen
01 / Reveal

Reveal sections once as they enter

RevealLive preview

Scroll the box to bring each section in. once stops observing after the first entry. Without JavaScript or IntersectionObserver the content stays visible.

First section
Second section
Third section
Try it
Explore the code

HTML / CDN

HTML

index.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

style.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

main.js
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

index.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>
    <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

index.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

style.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

main.js
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

index.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>
    <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>
02 / Lazy load

Load content just before it is needed

Lazy loadLive preview

Scroll the box: each story loads just before it arrives. rootMargin starts loading 200px before an item scrolls in. Each item loads exactly once.

Loading…
Loading…
Loading…
Loading…
Loading…
Try it
Explore the code

HTML / CDN

HTML

index.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

style.css
#feed {
  height: 14rem;
  overflow-y: auto;
  display: grid;
  gap: 10rem;
  padding: 1rem;
  border: 1px solid #ccc;
}

JavaScript

main.js
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

index.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>
    <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

index.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

style.css
#feed {
  height: 14rem;
  overflow-y: auto;
  display: grid;
  gap: 10rem;
  padding: 1rem;
  border: 1px solid #ccc;
}

JavaScript

main.js
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

index.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>
    <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>
03 / Pause off-screen

Run work only while it is on screen

Pause off-screenLive preview

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.

Running: 0

Try it
Explore the code

HTML / CDN

HTML

index.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

style.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

main.js
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

index.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>
    <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

index.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

style.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

main.js
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

index.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>
    <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
@samline/is-visible

Visibility callbacks for one or many elements on shared intersection observers, for vanilla JavaScript, React, Vue, Svelte and plain HTML.

v1.1.2·MIT

Explore

DocsInstallation guideWorking examples

Project

Is Visible skill for AI agentsGitHub(opens in a new tab)Changelognpm(opens in a new tab)License(opens in a new tab)

© 2026 samline · Made for the open web.

Back to top