Basic Usage
Basic Usage
Section titled “Basic Usage”Introduction
Section titled “Introduction”Create an Intersection Observer with a callback and options, then observe target elements.
Basic Setup
Section titled “Basic Setup”const observer = new IntersectionObserver((entries) => { entries.forEach(entry => { if (entry.isIntersecting) { console.log('Element is visible:', entry.target); observer.unobserve(entry.target); // Stop observing } });}, { root: null, // Viewport (or element reference) threshold: 0.1, // 10% visible before triggering rootMargin: '0px' // Expand/shrink the root area});
// Start observingconst target = document.querySelector('.lazy-image');observer.observe(target);Options
Section titled “Options”| Option | Default | Description |
|---|---|---|
root | null (viewport) | Container element to check intersection against |
threshold | 0 | Percentage visible (0-1) to trigger |
rootMargin | '0px' | Expand/shrink the root bounding box |
Observer Methods
Section titled “Observer Methods”observer.observe(element); // Start watching an elementobserver.unobserve(element); // Stop watching an elementobserver.disconnect(); // Stop watching all elementsSummary
Section titled “Summary”- Create with
new IntersectionObserver(callback, options) - Callback receives array of IntersectionObserverEntry objects
entry.isIntersectingtells if element is visibleobserver.unobserve()stops watching a specific element