Observe element intersection
Quick answer
IntersectionObserver reports changes in a target’s geometric intersection with a root. It supports visibility-related decisions such as loading nearby content without continuous manual rectangle polling.
How it works
Construct new IntersectionObserver(callback, {root, rootMargin, threshold}). A null root uses the viewport; an element root defines the relevant area. threshold values from 0 to 1 select intersection ratio boundaries, and rootMargin changes the root rectangle. Each entry supplies target, isIntersecting and intersectionRatio. Geometric intersection does not establish that a user actually saw the content, nor that it is unoccluded. observe registers a target; unobserve removes it and disconnect removes all targets. Use native lazy loading when appropriate.
Local examples
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {const o=new IntersectionObserver(()=>{}, {threshold:[0.75,0.25]});console.log(JSON.stringify(o.thresholds),o.root===null);o.disconnect();})()Expected console output:
[0.25,0.75] trueThe threshold list is sorted and the default root is the viewport.
Run in a fresh browser document with local synthetic data. Actual ByteMind editor support is unverified.
(async () => {const root=document.createElement("div");root.style.cssText="width:100px;height:100px;overflow:hidden";const child=document.createElement("div");child.style.cssText="width:40px;height:40px";root.append(child);document.body.append(root);let o;try{const result=await new Promise(resolve=>{o=new IntersectionObserver(([entry])=>resolve([entry.target===child,entry.isIntersecting,entry.intersectionRatio]),{root});o.observe(child);});console.log(JSON.stringify(result));}finally{o?.disconnect();root.remove();}})()Expected console output:
[true,true,1]A fully contained target in this fixture has intersection ratio one.
Common mistakes
Do not equate intersectionRatio with guaranteed visible pixels or expect synchronous notification immediately after observe.
Practice
Place a target inside a clipped 100px root, move it outside, and compare entry.isIntersecting across deliveries. Disconnect afterward.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Observe element intersection — Knowledge Check
Apply the concept and reason about local example output.
Which root is used when IntersectionObserver root is null?
Does geometric intersection prove a person saw an element?
Which operation removes all targets from an intersection observer?
What is the exact console output in Observe element intersection example 1?
(async () => {const o=new IntersectionObserver(()=>{}, {threshold:[0.75,0.25]});console.log(JSON.stringify(o.thresholds),o.root===null);o.disconnect();})()What is the exact console output in Observe element intersection example 2?
(async () => {const root=document.createElement("div");root.style.cssText="width:100px;height:100px;overflow:hidden";const child=document.createElement("div");child.style.cssText="width:40px;height:40px";root.append(child);document.body.append(root);let o;try{const result=await new Promise(resolve=>{o=new IntersectionObserver(([entry])=>resolve([entry.target===child,entry.isIntersecting,entry.intersectionRatio]),{root});o.observe(child);});console.log(JSON.stringify(result));}finally{o?.disconnect();root.remove();}})()