Lozad.js
Highly performant, light and configurable lazy loader in pure JS with no dependencies for images, iframes and more, using IntersectionObserver API
Lozad.js:
- lazy loads elements performantly using pure JavaScript,
- is a light-weight library, just minified & gzipped,
- has NO DEPENDENCIES :)
- allows lazy loading of dynamically added elements as well,
- supports <img>, <picture>, iframes, videos, audios, responsive images, background images and multiple background images etc.
- even supports LQIP (Low Quality Image Placeholder)
- is completely free and open source.
It is written with an aim to lazy load images, iframes, ads, videos or any other element using the recently added Intersection Observer API with tremendous performance benefits.
Featured in:
Brands using Lozad.js:
and many more...
Table of Contents
- Demo
- Background
- Install
- Usage
- Example with picture tag
- Browser Support
- FAQs
- Contribute
- Changelog
- License
Yet another Lazy Loading JavaScript library, why?
Existing lazy loading libraries hook up to the scroll event or use a periodic timer and call getBoundingClientRect()
on elements that need to be lazy loaded. This approach, however, is painfully slow as each call to getBoundingClientRect()
forces the browser to re-layout the entire page and will introduce considerable jank to your website.
Making this more efficient and performant is what IntersectionObserver is designed for, and it’s landed in Chrome 51. IntersectionObservers let you know when an observed element enters or exits the browser’s viewport.
Install
# You can install lozad with npm $ npm install --save lozad # Alternatively you can use Yarn $ yarn add lozad # Another option is to use Bower $ bower install lozad
Then with a module bundler like rollup or webpack, use as you would anything else:
// using ES6 modules // using CommonJS modulesvar lozad =
Or load via CDN and include in the head
tag of your page.
When loading from CDN, you can find the library on window.lozad
.
Usage
In HTML, add an identifier to the element (default selector identified is lozad
class):
All you need to do now is just instantiate Lozad as follows:
const observer = ; // lazy loads elements with default selector as '.lozad'observer;
or with a DOM Element
reference:
const el = document;const observer = ; // passing a `NodeList` (e.g. `document.querySelectorAll()`) is also validobserver;
or with custom options:
const observer = ;observer;
Reference:
or if you want to give custom function definition to load element:
;
If you would like to extend the loaded
state of elements, you can add the loaded option:
Note: The
"data-loaded"="true"
attribute is used by lozad to determine if an element has been previously loaded.
;
If you want to lazy load dynamically added elements:
const observer = ;observer; // ... code to dynamically add elementsobserver; // observes newly added elements as well
for use with responsive images
<!-- responsive image example -->
for use with background images
<!-- background image example -->
for use with multiple background images
<!-- multiple background image example -->
for use with responsive background images (image-set)
<!-- responsive background image-set example -->
To change the delimiter that splits background images:
<!-- custom delimiter for background images example -->
If you want to load the images before they appear:
const observer = ;observer; const coolImage = document// ... trigger the load of a image before it appears on the viewportobserver;
Large image improvment
Sometimes image loading takes a long time. For this case, you can add a placeholder background:
Lozad sets a placeholder background color of img element and users will see the fallback till the image loads.
Example with picture tag
Create a broken picture element structure.
IE browser don't support picture tag! You need to set
data-iesrc
attribute (only for your picture tags) with source for IE browser
data-alt
attribute can be added to picture tag for use inalt
attribute of lazy-loaded images
<!-- For an element to be caught, add a block type that is different from the inline and some min-height for correct caught into view --> <!-- NO img element --> <!-- instead of img element, there will be the last source with the minimum dimensions --> <!-- for disabled JS you can set <noscript><img src="images/thumbs/04.jpg" alt=""></noscript> -->
When lozad loads this picture element, it will fix it.
If you want to use image placeholder (like low quality image placeholder), you can set a temporary img
tag inside your picture
tag. It will be removed when lozad loads the picture element.
<!-- you can define a low quality image placeholder that will be removed when the picture is loaded -->
Example with video
Example with iframe
That's all, just add the lozad
class.
Example toggling class
<!-- content -->
The active
class will be toggled on the element when it enters the browser’s viewport.
Browser Support
Available in latest browsers. If browser support is not available, then make use of polyfill.
For IE11 support, please make use of these polyfills.
FAQs
Checkout the FAQ Wiki for some common gotchas to be aware of while using lozad.js
Contribute
Interested in contributing features and fixes?
Changelog
See the Changelog