API Types
👁️ Intersection Observer — Element Dikha Ya Nahi
new IntersectionObserver(callback, options)
Intersection Observer element ko watch karta hai aur batata hai jab woh viewport (screen ka visible area) mein enter ya leave karta hai. Jaise security guard jo report karta hai "koi building mein aaya" ya "koi chala gaya." Koi scroll event listener nahi chahiye — bahut performant hai.
new IntersectionObserver(callback, options) se observer banao. Callback mein entries aati hain with isIntersecting (visible ya nahi) aur intersectionRatio (0 = hidden, 1 = fully visible). observer.observe(element) se watch shuru karo.
Security Guard Analogy: Scroll event listener = guard jo har second har person check karta hai (wasteful). Intersection Observer = guard jo sirf tab report karta hai jab koi enter/exit kare (efficient). Same result, 90% kam kaam.
Code Example
HTML — Intersection Observer — Element Dikha Ya Nahi
<!-- Lazy load images -->
<img data-src="heavy-image.jpg" alt="Lazy loaded" class="lazy">
<script>
const lazyImages = document.querySelectorAll("img.lazy");
const imageObserver = new IntersectionObserver(
(entries, observer) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const img = entry.target;
img.src = img.dataset.src;
img.classList.remove("lazy");
observer.unobserve(img);
}
});
},
{ rootMargin: "100px" }
);
lazyImages.forEach(img => imageObserver.observe(img));
</script>
<!-- Infinite scroll -->
<div id="sentinel"></div>
<div id="content"></div>
<script>
let page = 1;
const sentinelObserver = new IntersectionObserver(
(entries) => {
if (entries[0].isIntersecting) {
loadMoreContent();
}
},
{ rootMargin: "200px" }
);
sentinelObserver.observe(document.getElementById("sentinel"));
async function loadMoreContent() {
const res = await fetch(`/api/items?page=${page}`);
const data = await res.json();
data.items.forEach(item => {
document.getElementById("content").innerHTML += `
<div class="item">${item.name}</div>`;
});
page++;
}
</script>
<!-- Scroll animation -->
<div class="animate-on-scroll">Content</div>
<script>
const animateObserver = new IntersectionObserver(
(entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add("visible");
animateObserver.unobserve(entry.target);
}
});
},
{ threshold: 0.2 }
);
document.querySelectorAll(".animate-on-scroll").forEach(el => {
animateObserver.observe(el);
});
</script>
/* CSS */
.animate-on-scroll {
opacity: 0;
transform: translateY(20px);
transition: all 0.6s ease;
}
.animate-on-scroll.visible {
opacity: 1;
transform: translateY(0);
}new IntersectionObserver()Observer banao — callback + optionscallback(entries)Visibility change pe fire — entries array check karoisIntersectingtrue = visible, false = hiddenintersectionRatio0 = fully hidden, 1 = fully visibleobserver.observe(el)Element pe watch shuru karoobserver.unobserve(el)Watch band karo — performance ke liye zaroori!rootMargin: "100px"Trigger area ko 100px expand/shrink karothreshold: 0.220% visibility pe trigger (0=any, 1=fully)data-srcReal src store karo — visible hone pe swap (lazy loading pattern)✅ When to Use
- Images aur iframes lazy load karna (huge performance win)
- Infinite scroll (user scroll near bottom pe more load karo)
- Scroll-triggered animations (scroll pe fade in)
- Analytics — "user ne ye section dekha ya nahi?" tracking
- Ad loading — sirf visible hone pe ads load karo
❌ When NOT to Use
- Exact pixel positions chahiye (getBoundingClientRect use karo)
- Bahut fast-changing positions (observer automatically debounces)
- Simple one-time check (ek baar getBoundingClientRect check karo)
Intersection Observer #1 performance optimization hai. Fold ke neeche EVERYTHING lazy load karo — images, iframes, heavy components.
rootMargin: "200px" use karo element visible hone se PEHLE loading start ho (preload while user scrolls toward it). Trigger ke baad HAMESHA unobserve() call karo unnecessary callbacks avoid karne ke liye.