Web APIs

IntersectionObserver.observe()

Try the free JavaScript compiler

Registers an Element for intersection notifications, including its initial observation.

Formal Syntax & Structure

javascript syntaxjavascript
observer.observe(target)

Parameters & Attributes

NameTypeDescription
targetElementElement to observe.

Return Value

undefined

Registers a target.

Working Example

Example Snippetjavascript
(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();}})()
JavaScript Reference