LazyLoad (vanilla-lazyload)
LazyLoad is a fast, lightweight and flexible script that speeds up your web application by loading images only as they enter the viewport. LazyLoad supports responsive images.
LazyLoad is a fast, lightweight and flexible script that speeds up your web application by loading your content images, videos and iframes only as they enter the viewport. It’s written in plain “vanilla” JavaScript, it leverages the IntersectionObserver API, it works with responsive images and it supports native lazy loading. See notable features for more.
➡️ Jump to: 👨💻 Getting started - HTML - 👩💻 Getting started - Script - 🥧 Recipes - 📺 Demos - 😋 Tips & tricks - 🔌 API - 😯 Notable features
👨💻 Getting started - HTML
In order to make your content be loaded by LazyLoad, you must use some
data-
attributes instead of the actual attributes. Examples below.Lazy image:
Lazy image with low quality placeholder:
Lazy responsive image with srcset
and sizes
:
To have a low quality placeholder, add the
src
attribute pointing to a very small version of the image. E.g. src="lazy_10.jpg"
.
Lazy responsive image with hi-dpi support using the picture
tag:
To have a low quality placeholder, add the
src
attribute pointing to a very small version of the image to the img
tag. E.g. src="lazy_10.jpg"
.
Lazy responsive image with automatic WebP format selection, using the picture
tag:
To have a low quality placeholder, add the
src
attribute pointing to a very small version of the image to the img
tag. E.g. src="lazy_10.jpg"
.Lazy background image
Single background
Multiple backgrounds
Notes:
- you need to use
url()
in the value of yourdata-bg
attribute, also for single background - you shouldn’t use background images to load content images, they’re bad for SEO and for accessibility
- on background images,
callback_loaded
won’t be called and theclass_loaded
class won’t be added
Lazy video
Lazy iframe
👩💻 Getting started - Script
The latest, recommended version of LazyLoad is 12.1.0.
To polyfill or not to polyfill IntersectionObserver?
On browser NOT supporting IntersectionObserver such as Internet explorer and older versions of Safari you can choose whether or not to add a javascript polyfill for it.
If you don’t use a polyfill, LazyLoad will load all the images as soon as it’s downloaded and executed. The number of impacted users would be relatively small, so this is a completely acceptable choice.
If you prefer to load a polyfill, the regular LazyLoad behaviour is granted.
The simple, easiest way
The easiest way to use LazyLoad is to include the script from a CDN:
Or, with the IntersectionObserver polyfill:
Then, in your javascript code:
To be sure that DOM for your lazy content is ready when you instantiate LazyLoad, place the script tag right before the closing
</body>
tag. If more DOM arrives later, e.g. via an AJAX call, you’ll need to call lazyLoadInstance.update();
to make LazyLoad check the DOM again.Include via RequireJS
You can use RequireJS to dynamically and asynchronously load modules in your website.
You can also find the original W3C’S IntersectionObserver Polyfill packed in AMD so you can
require
it conditionally, along with LazyLoad.
Include RequireJS:
Then
require
the AMD version of LazyLoad, like this:
Using an async
script
If you prefer, it’s possible to include LazyLoad’s script using
async
script and initialize it as soon as it’s loaded.
To do so, you must define the options before including the script. You can pass:
{}
an object to get a single instance of LazyLoad[{}, {}]
an array of objects to get multiple instances of LazyLoad, each one with different options.
Then include the script.
Possibly place the script tag right before the closing
</body>
tag. If you can’t do that, LazyLoad could be executed before the browser has loaded all the DOM, and you’ll need to call its update()
method to make it check the DOM again.
Using an async
script + getting the instance reference
Same as above, but you must put the
addEventListener
code shown below before including the async
script.
Then include the script.
Now you’ll be able to call its methods, like:
Note about Internet Explorer: because this technique uses a
CustomEvent
(learn more) to trigger the LazyLoad::Initialized
event, you might want to add this micro polyfill to make it work on Internet Explorer.Local install
If you prefer to install LazyLoad locally in your project, you can!
Using npm
Using bower
Manual download
Download one the latest releases. The files you need are inside the
dist
folder. If you don’t know which one to pick, use lazyload.min.js
, or read about bundles.Local usage
Should you install LazyLoad locally, you can import it as ES module like the following:
It’s also possible (but unadvised) to use the
require
commonJS syntax.
More information about bundling LazyLoad with WebPack are available on this specific repo.
Usage with React
Take a look at this example of usage of React with LazyLoad on Sandbox.
This implementation takes the same props that you would normally pass to the
img
tag, but it renders a lazy image. Feel free to fork and improve it!Bundles
Inside the
dist
folder you will find different bundles.Filename | Module Type | Advantages |
---|---|---|
lazyload.min.js | UMD (Universal Module Definition) | Works pretty much everywhere, even in common-js contexts |
lazyload.iife.min.js | IIFE (Immediately Invoked Function Expression) | Works as in-page <script src="..."> , ~0.5kb smaller than UMD version |
lazyload.amd.min.js | AMD (Asynchronous Module Definition) | Works with RequireJS module loader, ~0.5kb smaller than UMD version |
lazyload.esm.js | ES Module | Exports LazyLoad so you can import it in your project both using <script type="module" src="..."> and a bundler like WebPack or Rollup |
🥧 Recipes
This is the section where you can find ready to copy & paste code for your convenience.Dynamic content
💡 Use case: when you want to lazily load images, but the number of images change in the scrolling area changes, maybe because they are added asynchronously.HTML
The HTML to use depends on your case, see other recipes’ HTML
Javascript
Scrolling panel(s)
💡 Use case: when your scrolling container is not the main browser window, but a scrolling container.HTML
Javascript
HTML
Javascript
Delay loading
💡 Use case: if a your scrolls fast over your images, you might wait a short time before the images start loading. This is how.
HTML
Javascript
DEMO | SOURCE | API |
Lazy LazyLoad
💡 Use case: when you have a lot of scrolling containers in the page and you want to instantiate a LazyLoad only on the ones that are in the viewport.
HTML
Javascript
.horzContainer
element enters the viewport, LazyLoad calls the callback_enter
function, which creates a new instance of LazyLoad on the .horzContainer
element.