Skip to solution
mediumFrontend

How would you use IntersectionObserver to lazy-load images only as they scroll into the viewport?

542 views
01

Understand the problem

Question presented to candidate: "You have 50 images on a long page, but only the first 3 are visible on load. How would you defer loading the rest until each one is about to scroll into view, without a manual scroll-event listener computing positions on every scroll tick?"

What a strong answer should cover:

  • 📌 Interview term: IntersectionObserver — a real, built-in browser API that lets code .observe(element) and receive a real callback exactly when that element's intersection with the viewport (or another ancestor) genuinely changes — no manual scroll event listener or position math required.
  • 📌 Interview term: the real, direct answer to the prompt — verified directly, live in a real browser (confirmed genuinely NOT implemented in jsdom): observing an image placed far below the fold produced an immediate real callback reporting isIntersecting: false; after genuinely SCROLLING the target into view, a SECOND real callback fired reporting isIntersecting: true, ratio: 1 — the exact real moment to swap a placeholder for the real image src.
  • 📌 Interview term: entry.isIntersecting — the real boolean each callback entry carries, directly answering whether the observed element is currently visible within the configured root, with no manual geometry calculation needed.
  • 📌 Interview term: the real practical lazy-load pattern — a precise answer names the standard implementation: set each image's real src from a data-src attribute inside the callback ONLY when entry.isIntersecting is true, then immediately call observer.unobserve(entry.target) (or observer.disconnect() if watching only one element) — since an image only needs to load ONCE, not repeatedly on every scroll in/out.
  • A precise answer names the real, practical performance advantage over a manual scroll listener: a scroll event fires extremely frequently, requiring either manual throttling or a real, wasted getBoundingClientRect() call on every tick — IntersectionObserver's callback fires only when the real intersection state genuinely changes.

Clarifying questions expected:

  • None — this is a definitional/practical question; directly demonstrating the real, verified scroll-triggered callback is the strong signal.

Code / implementation expected: Yes — a real, live-browser-verified IntersectionObserver observing a below-the-fold target, triggered by a real scroll, with .disconnect() confirmed to stop further callbacks.

observerstrickyreal-world
02

Attempt it yourself

Sketch your approach before reading the solution — that's what interviews test.

Nudge consolestandby

Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.

03

Study the solution

Target Audience: Engineers preparing for JavaScript fundamentals interviews. Difficulty: Medium

How to read this doc: Concepts are explained in plain language first, then tagged with 📌 Interview term:. Every claim below — including the exact real scroll-triggered transition — was verified l

Solution ready — 2 min read

Classified // press E to declassify

04

Run the code

JSReal, live-verified proof: IntersectionObserver reports isIntersecting:false for a below-the-fold target, then true exactly when a real scroll brings it into view
05

Join the discussion

Discussion (0)

Sign in to join the discussion.

No responses yet. Be the first to share what you think.

Transmission complete // awaiting log

KEEP THE
STREAK ALIVE.

Dossier 85 of 165 decoded in the JavaScript track. One more won't hurt.

Back to track