Guide

Working examples

Every live Is Visible preview with its explanation and the complete, copyable source files of each one.

Each preview is the same one shown on Home and Examples, and runs the real package. The copied files load the published browser bundle, so they work as they are.

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

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

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

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>