explaingit

apoorvsaxena/lozad.js

7,498JavaScriptAudience · developerComplexity · 1/5LicenseSetup · easy

TLDR

A tiny, zero-dependency JavaScript library that uses the browser's built-in Intersection Observer to lazy-load images, videos, and iframes only when they scroll into view.

Mindmap

mindmap
  root((lozad.js))
    What it does
      Delays loading off-screen content
      Speeds up initial page load
      Saves user bandwidth
    How it works
      Intersection Observer API
      Data attribute for real URL
      CSS class to mark elements
    Supported elements
      Images and responsive images
      Videos and audio
      Iframes
    Features
      Dynamic element support
      Custom threshold settings
      Background image support
    Audience
      Web developers
      Frontend engineers
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

Things people build with this

USE CASE 1

Speed up a photo gallery page by lazy-loading hundreds of images so only visible ones download on arrival.

USE CASE 2

Reduce bandwidth costs on a video platform by delaying iframe embeds until the user scrolls near them.

USE CASE 3

Add lazy-loaded background images to a marketing landing page without any extra dependencies.

USE CASE 4

Improve Core Web Vitals scores on an e-commerce product listing by deferring off-screen product images.

Tech stack

JavaScript

Getting it running

Difficulty · easy Time to first run · 5min

No dependencies, add a class to elements, a data-src attribute, and two lines of JavaScript, works immediately.

Open source under a permissive license, use freely in any project including commercial ones.

In plain English

Lozad.js is a small JavaScript library that delays loading images, videos, iframes, and other page elements until they are about to appear on the screen. This technique is called lazy loading, and it speeds up a webpage by avoiding the download of content the visitor has not yet scrolled to. On a long page with many images, this can significantly reduce initial load time and save bandwidth. Most older lazy loading libraries work by listening to the scroll event and repeatedly checking whether each element is close to the visible area. This approach has a performance cost because it forces the browser to measure the position of every tracked element on each scroll event, which can make the page feel sluggish. Lozad.js takes a different approach: it uses the browser's Intersection Observer API, which was designed specifically for this purpose. The browser does the work of noticing when an element enters the viewport, and Lozad.js responds to that notification instead of polling constantly. Using the library is straightforward. You add a class to any element you want to lazy load and store the real URL in a data attribute instead of the normal source attribute. Then you create a Lozad observer with a couple of lines of JavaScript and call its observe method. From that point on, elements are loaded automatically as the user scrolls toward them. The library supports images, responsive images with multiple size variants, iframes, videos, audio elements, background images, and multiple background images on a single element. It also works with elements added to the page dynamically after the initial load, and it can be customized with options for how close to the viewport an element needs to be before loading begins. Lozad.js has no dependencies, weighs about one kilobyte compressed, and is used in production by companies including Tesla, Binance, and Verizon. It is free and open source.

Copy-paste prompts

Prompt 1
Show me how to set up lozad.js on a webpage so that all images below the fold are lazy-loaded as the user scrolls.
Prompt 2
How do I use lozad.js to lazy-load responsive images with multiple srcset sizes?
Prompt 3
Write the minimal HTML and JavaScript needed to lazy-load an iframe embed using lozad.js.
Prompt 4
How can I configure lozad.js to start loading images when they are 200 pixels away from the viewport edge?
Prompt 5
Show me how to make lozad.js observe new images added to the page dynamically via JavaScript.
Open on GitHub → Explain another repo

← apoorvsaxena on gitmyhub — every repo by this author, as a profile.

Verify against the repo before relying on details.