IntersectionObserver API and Thresholds
Create observers and configure visibility thresholds.
IntersectionObserver API and Thresholds is a free HTML 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 HTML Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
What IntersectionObserver Does
IntersectionObserver fires callbacks when an element enters or leaves the viewport (or another scroll container). It is the modern, performant alternative to scroll listeners — the browser does the math, your code only runs on actual visibility transitions.
Construction
new IntersectionObserver(callback, options) creates an observer. The callback receives an array of entries when intersections change; options control which container is observed and at what thresholds.
const observer = new IntersectionObserver((entries) => {
for (const entry of entries) {
if (entry.isIntersecting) {
console.log("Visible:", entry.target);
}
}
});
observer.observe(document.querySelector(".hero"));The IntersectionObserverEntry
Each entry has target (the observed element), isIntersecting (boolean), intersectionRatio (0 to 1), boundingClientRect, intersectionRect, and time. Use isIntersecting for binary visibility checks; intersectionRatio for percent-visible logic.
The threshold Option
{ threshold: 0.5 } fires the callback whenever the intersection ratio crosses 50%. Use an array [0, 0.25, 0.5, 0.75, 1] to fire at every quarter — useful for building progress indicators or analytics that report "% visible".
The root Option
By default, the observer measures intersection with the viewport. Set root: someScrollableDiv to measure against an inner scroll container — for example, a virtualized list inside a fixed-height div.
The rootMargin Option
rootMargin: "200px" grows the observation area by 200 pixels on every side. Use it to fire earlier (start loading images 200px before they enter the viewport) or later (only fire when fully past the viewport).
new IntersectionObserver(callback, {
rootMargin: "0px 0px 200px 0px",
threshold: 0
});observe and unobserve
observer.observe(element) starts watching an element; observer.unobserve(element) stops. Always unobserve when you no longer need the callback (e.g., after lazy-loading triggers once) to free memory.
disconnect
observer.disconnect() stops watching all observed elements. Call when the component is unmounted or the observer is no longer needed — prevents leaks from observers that outlive their use case.
Async Delivery
Callbacks are delivered asynchronously and batched — many intersection changes during a fast scroll fire one callback with all entries, not one per element. This is a major performance win over scroll listeners that fire on every scroll event.
Initial Callback Fires Immediately
When you observe a new element, the callback fires once almost immediately with the current state — useful for handling elements that are already visible at the time observation begins, without a special initialization path.
Common Use Cases
Lazy-loading images, triggering animations on scroll into view, infinite scrolling (observe a sentinel near the list end), tracking which articles a user has actually read, sticky-header toggling based on hero visibility.
Knowledge Check
What is the difference between threshold: 0 and threshold: 1 in an IntersectionObserver?
Summary
IntersectionObserver fires callbacks on element visibility transitions with built-in performance and batching — replace scroll listeners with it for visibility logic. Configure with threshold (0 to 1, or array), root (custom scroll container), and rootMargin (grow/shrink observed area). observe/unobserve specific elements; disconnect cleans up everything.
Frequently asked questions
Is the “IntersectionObserver API and Thresholds” lesson free?
Yes — the full text of “IntersectionObserver API and Thresholds” is free to read here on the web, and the HTML 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 HTML Academy course, upgrade to CoddyKit PRO.
What will I learn in “IntersectionObserver API and Thresholds”?
Create observers and configure visibility thresholds. You practise HTML 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 HTML Academy?
No prior experience is required. HTML 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 “IntersectionObserver API and Thresholds” 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 HTML Academy lesson?
Yes. Every HTML 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
- IntersectionObserver API and Thresholds
- Lazy Loading with IntersectionObserver
- Infinite Scroll Implementation
- Scroll-Linked Animations Setup