explaingit

callmecavs/layzr.js

5,508JavaScriptAudience · developerComplexity · 2/5LicenseSetup · easy

TLDR

A small JavaScript library that delays image loading until each image is almost visible on screen, making image-heavy pages feel faster by not downloading everything at once.

Mindmap

mindmap
  root((repo))
    What it does
      Defers image load
      Loads near viewport
      Picks best source
    Tech Stack
      JavaScript
      npm
      HTML data attrs
    Use Cases
      Photo-heavy pages
      Responsive images
      Dynamic galleries
    Features
      Threshold control
      Before and after events
      Dynamic refresh API
    Browser Support
      Chrome 24 plus
      Firefox 23 plus
      IE 10 plus
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 an image-heavy blog or portfolio site by loading images only as the user scrolls toward them

USE CASE 2

Serve the right image size automatically on retina and standard screens using data-retina and data-srcset attributes

USE CASE 3

Show a loading spinner before each image appears by hooking into the before-load event Layzr fires

Tech stack

JavaScriptnpm

Getting it running

Difficulty · easy Time to first run · 30min
MIT license, use freely for any purpose, including commercial projects, as long as you keep the copyright notice.

In plain English

Layzr.js is a JavaScript library that delays image loading on a web page until each image is about to scroll into view. Instead of downloading every image the moment a page opens, the browser waits until the user is close to seeing an image, then fetches it. This reduces how much data loads upfront, which makes the initial page feel faster, particularly on pages with many photos. The setup involves adding special data attributes to your HTML image tags rather than the standard src attribute. You put the image file path in an attribute called data-normal, and optionally add a data-retina path for high-resolution screens or a data-srcset value for responsive images that serve different sizes to different devices. Layzr reads these attributes and picks the most appropriate source for each visitor's browser and screen. Once the library is installed via npm and imported into your JavaScript, you create an instance with one line. You can also pass a threshold option, which controls how far above or below the viewport edge images start loading. A positive threshold causes images to load before they fully scroll into view, which can feel smoother for the reader. The library emits two events: one just before it sets an image source and one just after. These let you attach your own code at each moment, for example to show a loading spinner or log analytics. The API also includes methods to manually trigger a check of which images are in view and to refresh the list of images being tracked, which is useful when new images are added to the page dynamically after it first loads. Layzr supports all major browsers back to Chrome 24, Firefox 23, Safari 6.1, and Internet Explorer 10. The library is released under the MIT license.

Copy-paste prompts

Prompt 1
I have a product gallery page with 80 images and I want to lazy-load them with Layzr.js. Show me the HTML data attributes I need on each img tag and the JavaScript setup code.
Prompt 2
My page adds new product images dynamically via JavaScript. How do I tell Layzr.js to start tracking these new images after the page has already loaded?
Prompt 3
How do I configure Layzr.js to start fetching images 300 pixels before they scroll into view so there is no visible pop-in for the user?
Prompt 4
I want to add a CSS fade-in effect when Layzr loads each image. Show me how to use the load event to add and remove a CSS class on the image element.
Open on GitHub → Explain another repo

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

Verify against the repo before relying on details.