Baike.dev
All toolsOnline toolsAI codingTrendingOpen sourceNewsSubmit
Log in
< Back to tools
L

lazyload

> 编程语言
Open source

Vanilla JavaScript plugin for lazyloading images

8.7K stars0 likes2 views
WebsiteGitHub

About

Vanilla JavaScript plugin for lazyloading images

Lazy Load Remastered

Lazy Load delays loading of images in long web pages. Images outside of viewport will not be loaded before user scrolls to them. This is opposite of image preloading.

This is a modern vanilla JavaScript version of the original Lazy Load plugin. It uses Intersection Observer API to observe when the image enters the browsers viewport. Original code was inspired by YUI ImageLoader utility by Matt Mlinac. New version loans heavily from a blog post by Dean Hume.

Basic Usage

By default Lazy Load assumes the URL of the original high resolution image can be found in data-src attribute. You can also include an optional low resolution placeholder in the src attribute.

xml
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lazyload.js"></script>

With the HTML in place you can then initialize the plugin using the factory method. If you do not pass any settings or image elements it will lazyload all images with class lazyload.

javascript
lazyload();

If you prefer you can explicitly pass the image elements to the factory. Use this for example if you use different class name.

javascript
let images = document.querySelectorAll(".branwdo");
lazyload(images);

If you prefer you can also use the plain old constructor.

javascript
let images = document.querySelectorAll(".branwdo");
new LazyLoad(images);

The core IntersectionObserver can be configured by passing an additional argument

javascript
new LazyLoad(images, {
     root: null,
     rootMargin: "0px",
     threshold: 0
});

Additional API

To use the additional API you need to assign the plugin instance to a variable.

javascript
let lazy = lazyload();

To force loading of all images use loadimages().

javascript
lazy->loadImages();

To destroy the plugin and stop lazyloading use destroy().

javascript
lazy->destroy();

Note that destroy() does not load the out of viewport images. If you also want to load the images use loadAndDestroy().

javascript
lazy->loadAndDestroy();

Additional API is not avalaible with the jQuery wrapper.

jQuery Wrapper

If you use jQuery there is a wrapper which you can use with the familiar old syntax. Note that to provide BC it uses data-original by default. This should be a drop in replacement for the previous version of the plugin.

xml
javascript
$("img.lazyload").lazyload();

Cookbook

Blur Up Images

Low resolution placeholder ie. the "blur up" technique. You can see this in action in this blog entry. Scroll down to see blur up images.

xml
xml
<div class="lazyload"
     style="background-image: url('thumbnail.jpg')"
     data-src="original.jpg" />

Responsive Images

Lazyloaded Responsive images are supported via data-srcset. If browser does not support srcset and there is no polyfill the image from data-src will be shown.

xml
xml

Inlined Placeholder Image

To reduce the amount of request you can use data uri images as the placeholder.

xml

Install

This is still work in progress. You can install beta version with yarn or npm.

$ yarn add lazyload
$ npm install lazyload

License

All code licensed under the MIT License. All images licensed under Creative Commons Attribution 3.0 Unported License. In other words you are basically free to do whatever you want. Just don't remove my name from the source.

Issues· 109 open

View all issuesOpen on GitHub
  • #460

    Negative rootMargin

    Updated May 15, 2025
  • #397

    Cannot set property 'settings' of undefined

    Updated Dec 18, 2024
  • #457

    Publishing older versions to NPM request

    Updated Jun 28, 2024
  • #449

    Demo link is not working

    Updated Dec 10, 2023
  • #455

    How to use Lazyload with images in scrollable DIV? What is root for?

    Updated Oct 18, 2023
  • #410

    Doesn't work with Pinterest

    Updated Feb 7, 2023
  • #424

    Is this still maintained?

    Updated Oct 4, 2022
  • #447

    Does lazyload support <picture> source media?

    Updated Jul 27, 2022
  • #454

    Images Not Loading on IOS

    Updated Jul 24, 2021
  • #446

    WindowsWechat Not fully supported IntersectionObserver,isIntersecting attribute is undefined

    Updated Apr 2, 2021

> Tags

JavaScript

No comments yet. Be the first to share.

> Details

PublishedAug 1, 2026
UpdatedSep 29, 2026
Category编程语言
PricingOpen source

> Related tools

T
TypeScript
JavaScript 的超集,为前端与全栈提供静态类型
P
Python
通用编程语言,广泛用于 Web、数据与 AI
G
Go
Google 推出的简洁高效系统语言