MSK Institute LogoMSK.Institute
HomeCourses
Book Free Demo➔Sign In
Free Demo➔
MSK Institute LogoMSK.Institute

Leading computer training academy in Shikohabad for software engineering, Python, web development, and digital literacy.

ISO 9001:2015 Certified Center
Follow & Connect
4.9

Programs & Admissions

  • All CoursesCatalog
  • Learning PathsRoadmaps
  • Student ProjectsShowcase
  • Live Classes & ScheduleLive
  • Upcoming Batches
  • Campuses & LocationsHub
  • Verify Certificates
  • Book Free Demo Class

Resources & Tools

  • Free Online Mock TestsAll Tests
  • NIELIT CCC Mock TestFree
  • Course & Career FinderQuiz
  • Study Material Hub
  • All MSK ToolsHub
  • Keyboard ShortcutsNew
  • TypeQuest Speed LabTyping
  • MSK Code PlaygroundCode
  • Interactive Tutorials
  • Quick Cheatsheets

Institute & Contact

  • About MSK Institute
  • Careers & InternshipsHiring
  • Publications & Blogs
  • Gali No. 3, Near Gyan Jyoti Public School,
    Adarsh Nagar, Shikohabad, Uttar Pradesh 283135
  • +91 83930 42166
  • mskshikohabad@gmail.com
  • Hours:Mon – Sat: 8:00 AM – 7:00 PM | Sun: 10:00 AM – 2:00 PM
  • 4.9 ★(38 Google Reviews)
© 2026 MSK Institute. All rights reserved.•Privacy Policy•Terms of Service•Disclaimer•Contact Us

Shikohabad's Leading Computer & Coding Academy

Home
Courses
Live
Material
ResizeObserver & IntersectionObserver APIs0%
Study Materials
JavaScript for AdvancedProgress 0%
1.1Higher-Order Functions & Currying1.2Function Composition & Pipelining1.3Memoization & Pure Functions1.4Execution Context & Call Stack Deep Dive1.5Scope Chain & Lexical Environment
6.1Shadow DOM & Web Components Basics6.2Custom Elements Lifecycle (connectedCallback)6.3Virtual DOM Concepts & Reconciliation6.4MutationObserver API6.5ResizeObserver & IntersectionObserver APIs
HomeTutorialsJavaScript for AdvancedResizeObserver & IntersectionObserver APIs
IndexedDB Architecture & Transactions

ResizeObserver & IntersectionObserver APIs

Beginner12 min read•Updated: 2026-11-18•By Er. Sumit Kumar (MSK Institute)
Study Materials

Historically, detecting when an element became visible on screen or changed dimensions required attaching expensive scroll and resize listeners to window with heavy debouncing. The modern standard Web APIs—IntersectionObserver and ResizeObserver—perform these calculations directly in the browser's native compositing pipeline with high efficiency.


1. IntersectionObserver: Visibility & Viewport Tracking

IntersectionObserver detects when a target element intersects with an ancestor element or the top-level document viewport.

1 Browser Viewport (Window)
2 ┌─────────────────────────────────┐
3 │ │
4 │ Target Element (Below Fold) │
5 │ [ 0% Visible: Ratio 0.0 ] │
6 └─────────────────────────────────┘
7 │ (User scrolls down)
8 ┌───────────────▼─────────────────┐
9 │ [ 50% Visible: Ratio 0.5 ] │ ──► Callback triggers!
10 └─────────────────────────────────┘

High-Performance Lazy-Loading Images:

1const imageObserver = new IntersectionObserver((entries, observer) => {
2 entries.forEach(entry => {
3 // If the image is within viewport threshold:
4 if (entry.isIntersecting) {
5 const img = entry.target;
6 img.src = img.dataset.src; // Replace placeholder with real URL
7 img.classList.add('loaded');
8 observer.unobserve(img); // Cease observing once loaded!
9 }
10 });
11}, {
12 root: null, // Defaults to browser viewport
13 rootMargin: '200px', // Pre-fetch image 200px BEFORE it enters screen!
14 threshold: 0.1 // Trigger when 10% visible
15});
16 
17document.querySelectorAll('img[data-src]').forEach(img => {
18 imageObserver.observe(img);
19});

2. Infinite Scrolling with IntersectionObserver

Instead of calculating scroll height mathematics, place an empty "sentinel" <div> at the bottom of the feed:

1const sentinel = document.querySelector('#scroll-sentinel');
2 
3const infiniteScrollObserver = new IntersectionObserver((entries) => {
4 if (entries[0].isIntersecting) {
5 loadNextPageOfData();
6 }
7});
8 
9infiniteScrollObserver.observe(sentinel);

3. ResizeObserver: Element-Level Responsive Queries

While CSS @media queries only evaluate the entire browser window width, modern component design requires styling widgets based on their own container width (Container Queries). ResizeObserver monitors an individual element's box model:

1const widget = document.querySelector('.analytics-widget');
2 
3const resizeObserver = new ResizeObserver((entries) => {
4 for (const entry of entries) {
5 // Read precise content box dimensions
6 const width = entry.contentRect.width;
7
8 // Dynamically adjust layout classes based on element width!
9 if (width < 400) {
10 widget.classList.add('compact-view');
11 widget.classList.remove('wide-view');
12 } else {
13 widget.classList.add('wide-view');
14 widget.classList.remove('compact-view');
15 }
16 }
17});
18 
19resizeObserver.observe(widget);

4. Comparison Matrix

FeatureIntersectionObserverResizeObserver
MonitorsRelative visibility / viewport overlapElement boundary / contentRect size changes
Key Use CasesLazy loading images, infinite scroll, ad viewabilityContainer queries, responsive charts, canvas redraws
PerformanceOff-main-thread compositing engineBatched before paint; prevents layout loops

Interactive Quiz• Knowledge Check

Practice Quiz

Question 1
What is the primary performance benefit of IntersectionObserver over listening to window.addEventListener('scroll')?
Question 2
In an IntersectionObserver options object, what does rootMargin: '200px' do?
Question 3
What method should be called on an IntersectionObserver instance once a target image has successfully loaded?
Question 4
Why is ResizeObserver preferred over window.addEventListener('resize') for responsive components?
Question 5
What threshold value ensures an IntersectionObserver callback fires when half of the element is visible?
Next Lesson

IndexedDB Architecture & Transactions

Continue learning with hands-on practice, examples, and exercises in the upcoming topic.

Related Lessons

Previous LessonNext Lesson
MutationObserver APIIndexedDB Architecture & Transactions

Practice Quiz

Test your understanding of this lesson with 5 questions. Each question has one correct answer.

Mentor-Led Practical Training

Master JavaScript for Advanced with Hands-On Projects

Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.

View Full CourseBook Free Demo
PreviousNext
PreviousPrevNext