javascript
39 lines · 8 steps
How lazy image loading works
Defer loading images until they near the viewport using IntersectionObserver, with a graceful fallback.
Explained by
highlit
1function initLazyLoad(root = document) {
2 const images = root.querySelectorAll('img[data-src]');
3
4 if (!('IntersectionObserver' in window)) {
5 images.forEach(loadImage);
6 return;
7 }
8
9 const observer = new IntersectionObserver((entries, obs) => {
10 for (const entry of entries) {
11 if (!entry.isIntersecting) continue;
12 loadImage(entry.target);
13 obs.unobserve(entry.target);
14 }
15 }, {
16 rootMargin: '200px 0px',
17 threshold: 0.01
18 });
19
20 images.forEach((img) => observer.observe(img));
21}
22
23function loadImage(img) {
24 const { src, srcset } = img.dataset;
25
26 img.addEventListener('load', () => {
27 img.classList.add('is-loaded');
28 }, { once: true });
29
30 img.addEventListener('error', () => {
31 img.classList.add('is-error');
32 }, { once: true });
33
34 if (srcset) img.srcset = srcset;
35 if (src) img.src = src;
36
37 delete img.dataset.src;
38 delete img.dataset.srcset;
39}
01 / 01
STEP 01
‹ swipe to step through ›
Walkthrough
Space play
←→ step
click any line
Three takeaways
- 1IntersectionObserver lets you react to viewport proximity without scroll listeners or manual math.
- 2Feature-detect and fall back so the page works even where the API is missing.
- 3Unobserving after a hit and using once listeners keeps work one-shot and avoids leaks.
Related explainers
javascript
'use server' import { revalidatePath } from 'next/cache' import { redirect } from 'next/navigation'
How a Next.js Server Action updates a post
server-actions
authorization
validation
Intermediate
7 steps
javascript
const express = require('express'); const v1 = express.Router();
Versioning an API with Express Routers
api versioning
routing
modularity
Intermediate
10 steps
javascript
const RATE_LIMIT = 100; const WINDOW_MS = 60 * 1000; const BLOCK_MS = 5 * 60 * 1000;
Building a rate-limiting middleware in Express
rate-limiting
middleware
closures
Intermediate
9 steps
javascript
const transitions = { cart: { checkout: 'shipping' }, shipping: { submitAddress: 'payment', back: 'cart' }, payment: { submitPayment: 'review', back: 'shipping' },
A finite state machine for checkout flow
state-machine
event-driven
data-driven-design
Intermediate
7 steps
javascript
import { useState, useEffect, useCallback, useRef } from "react"; export function usePersistentForm(storageKey, initialValues) { const [values, setValues] = useState(() => {
A React hook that persists form state to localStorage
custom-hooks
localstorage
debounce
Intermediate
9 steps
javascript
function groupBy(items, keySelector) { const resolveKey = typeof keySelector === 'function' ? keySelector : (item) => item[keySelector];
Building a flexible groupBy in JavaScript
higher-order-functions
reduce
data-transformation
Intermediate
6 steps
Share this explainer
Here's the card — post it anywhere.
Made with highlit — turn any snippet into a walkthrough like this in about a minute.
Explain your code
Embed this explainer
Drop the interactive walkthrough into a blog or docs. Views never cost a credit.
<iframe src="https://highlit.co/explainers/how-lazy-image-loading-works-explained-javascript-24ee/embed?autoplay=1" width="100%" height="520" loading="lazy" style="border:0"></iframe>
Autoplay is on by default — add ?autoplay=0 to start paused.