0Pricing
JavaScript Academy · Lesson

Observing Element Visibility

Create an observer and watch elements.

Observing Element Visibility is a free JavaScript Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the JavaScript Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Why Intersection Observer?

Traditionally, detecting when an element enters the viewport meant listening to scroll and calling getBoundingClientRect constantly, which is slow and janky.

The Intersection Observer API watches visibility efficiently and asynchronously, off the main thread.

Creating an Observer

You create an observer with new IntersectionObserver(callback). The callback runs whenever a watched element's visibility crosses a threshold.

const observer = new IntersectionObserver((entries) => {
  entries.forEach(entry => {
    console.log(entry.target, entry.isIntersecting);
  });
});

Observing an Element

Call observer.observe(element) to start watching a target. One observer can watch many elements.

const box = document.querySelector('.box');
observer.observe(box);

// Watch several at once:
document.querySelectorAll('.card')
  .forEach(el => observer.observe(el));

The isIntersecting Flag

Each entry has a boolean isIntersecting that tells you whether the target currently overlaps the root (viewport by default).

const observer = new IntersectionObserver((entries) => {
  entries.forEach(entry => {
    if (entry.isIntersecting) {
      console.log('element is now visible');
    }
  });
});

Entry Details

Each IntersectionObserverEntry carries useful data:

  • target — the observed element
  • isIntersecting — whether it crosses the root
  • intersectionRatio — how much is visible (0 to 1)
  • boundingClientRect — the target's rectangle
(entries) => {
  entries.forEach(e => {
    console.log('visible portion:', e.intersectionRatio);
  });
}

The threshold Option

The threshold option controls at what visibility ratios the callback fires. 0 fires as soon as one pixel is visible; 1 fires only when fully visible.

const observer = new IntersectionObserver(callback, {
  threshold: 0.5 // fire when 50% visible
});

// You can pass an array for multiple steps:
// threshold: [0, 0.25, 0.5, 0.75, 1]

The root Option

By default the observer measures against the browser viewport. Set root to a scrollable ancestor element to measure visibility within that container instead.

const list = document.querySelector('.scroll-area');
const observer = new IntersectionObserver(callback, {
  root: list // measure visibility inside this container
});

The rootMargin Option

rootMargin grows or shrinks the root's bounding box, like CSS margins. Positive values trigger the callback earlier (before the element is actually on screen).

const observer = new IntersectionObserver(callback, {
  rootMargin: '200px' // start reacting 200px before visible
});

Stopping Observation

Call observer.unobserve(element) to stop watching one element, or observer.disconnect() to stop watching all of them. This is important for cleanup and one-time triggers.

const observer = new IntersectionObserver((entries) => {
  entries.forEach(entry => {
    if (entry.isIntersecting) {
      doSomething(entry.target);
      observer.unobserve(entry.target); // fire once
    }
  });
});

Why It Is Efficient

The browser computes intersections internally and batches notifications, instead of running your JavaScript on every scroll frame. This avoids layout thrashing and keeps scrolling smooth even with many targets.

Common Use Cases

Intersection Observer powers many real-world features:

  • Lazy loading images and iframes
  • Infinite scroll
  • Scroll-triggered animations
  • Tracking ad or content impressions

We will build several of these next.

Quick Check

Test your understanding of the Intersection Observer basics.

Recap

You learned the Intersection Observer basics:

  • new IntersectionObserver(callback, options) creates one.
  • observe starts watching; unobserve/disconnect stop it.
  • Each entry exposes isIntersecting and intersectionRatio.
  • threshold, root, and rootMargin tune when it fires.
  • It is far more efficient than scroll listeners.

Next, we lazy-load images with it.

Frequently asked questions

Is the “Observing Element Visibility” lesson free?

Yes — the full text of “Observing Element Visibility” is free to read here on the web, and the JavaScript Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the JavaScript Academy course, upgrade to CoddyKit PRO.

What will I learn in “Observing Element Visibility”?

Create an observer and watch elements. You practise JavaScript Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start JavaScript Academy?

No prior experience is required. JavaScript Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Observing Element Visibility” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this JavaScript Academy lesson?

Yes. Every JavaScript Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Observing Element Visibility
  2. Lazy Loading Images
  3. Infinite Scrolling
  4. Scroll-Triggered Animations
← Back to JavaScript Academy