Lozad

Highly performant, light ~1kb and configurable lazy loader in pure JS with ...

README

Lozad.js npm version Build Status npm undefined


Highly performant, light and configurable lazy loader in pure JS with no dependencies for images, iframes and more, using IntersectionObserver API


lozad.js lazy loading javascript library

Lozad.js:
- lazy loads elements performantly using pure JavaScript,
- is a light-weight library, just undefined 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 will reload when the valid attributes change.

It is written with an aim to lazy load images, iframes, ads, videos or any other element using the recently added Intersection Observer API and MutationObserver with tremendous performance benefits.

Featured in:



Brands using Lozad.js:


Tesla          Binance          Domino's         
BNP Paribas          Mi         
Amway          TATA          Verizon
Atlassian BNP Paribas
JLM Couture          New Balance          BBC

and many more...

Table of Contents



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


  1. ```sh
  2. # You can install lozad with npm
  3. $ npm install --save lozad

  4. # Alternatively you can use Yarn
  5. $ yarn add lozad

  6. # Another option is to use Bower
  7. $ bower install lozad
  8. ```

Then with a module bundler like rollup or webpack, use as you would anything else:

  1. ``` js
  2. // using ES6 modules
  3. import lozad from 'lozad'

  4. // using CommonJS modules
  5. var lozad = require('lozad')
  6. ```

Or load via CDN and include in the head tag of your page.

  1. ``` html
  2. <script type="text/javascript" src="https://cdn.jsdelivr.net/npm/lozad/dist/lozad.min.js"></script>
  3. ```

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):
  1. ``` html
  2. <img class="lozad" data-src="image.png">
  3. ```

All you need to do now is just instantiate Lozad as follows:
  1. ``` js
  2. const observer = lozad(); // lazy loads elements with default selector as '.lozad'
  3. observer.observe();
  4. ```
or with a DOM Element reference:
  1. ``` js
  2. const el = document.querySelector('img');
  3. const observer = lozad(el); // passing a `NodeList` (e.g. `document.querySelectorAll()`) is also valid
  4. observer.observe();
  5. ```
or with custom options:
  1. ``` js
  2. const observer = lozad('.lozad', {
  3.     rootMargin: '10px 0px', // syntax similar to that of CSS Margin
  4.     threshold: 0.1, // ratio of element convergence
  5.     enableAutoReload: true // it will reload the new image when validating attributes changes
  6. });
  7. observer.observe();
  8. ```
Reference:


or if you want to give custom function definition to load element:
  1. ``` js
  2. lozad('.lozad', {
  3.     load: function(el) {
  4.         console.log('loading element');

  5.         // Custom implementation to load an element
  6.         // e.g. el.src = el.getAttribute('data-src');
  7.     }
  8. });
  9. ```

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.

  1. ``` js
  2. lozad('.lozad', {
  3.     loaded: function(el) {
  4.         // Custom implementation on a loaded element
  5.         el.classList.add('loaded');
  6.     }
  7. });
  8. ```

If you want to lazy load dynamically added elements:

  1. ``` js
  2. const observer = lozad();
  3. observer.observe();

  4. // ... code to dynamically add elements
  5. observer.observe(); // observes newly added elements as well
  6. ```

for use with responsive images

  1. ``` html
  2. <img class="lozad" data-src="image.png" data-srcset="image.png 1000w, image-2x.png 2000w">
  3. ```

for use with background images
  1. ``` html
  2. <div class="lozad" data-background-image="image.png">
  3. </div>
  4. ```

for use with multiple background images
  1. ``` html
  2. <div class="lozad" data-background-image="path/to/first/image,path/to/second/image,path/to/third/image">
  3. </div>
  4. ```

for use with responsive background images (image-set)
  1. ``` html
  2. <div class="lozad" data-background-image-set="url('photo.jpg') 1x, url('photo@2x.jpg') 2x">
  3. </div>
  4. ```

To change the delimiter that splits background images:
  1. ``` html
  2. <div
  3.   class="lozad"
  4.   data-background-image="/first/custom,image,path/image.png-/second/custom,image,path/image.png"
  5.   data-background-delimiter="-"
  6. >
  7. </div>
  8. ```

If you want to load the images before they appear:

  1. ``` js
  2. const observer = lozad();
  3. observer.observe();

  4. const coolImage = document.querySelector('.image-to-load-first');
  5. // ... trigger the load of a image before it appears on the viewport
  6. observer.triggerLoad(coolImage);
  7. ```

Large image improvment


Sometimes image loading takes a long time. For this case, you can add a placeholder background:

  1. ``` html
  2. <img class="lozad" data-placeholder-background="red" data-src="image.png">
  3. ```

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 in alt attribute of lazy-loaded images


  1. ``` html
  2. <picture class="lozad" style="display: block; min-height: 1rem" data-iesrc="images/thumbs/04.jpg" data-alt="">
  3.     <source srcset="images/thumbs/04.jpg" media="(min-width: 1280px)">
  4.     <source srcset="images/thumbs/05.jpg" media="(min-width: 980px)">
  5.     <source srcset="images/thumbs/06.jpg" media="(min-width: 320px)">
  6.     
  7.     
  8.     
  9. </picture>
  10. ```

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.

  1. ``` html
  2. <picture class="lozad" style="display: block; min-height: 1rem" data-iesrc="images/thumbs/04.jpg" data-alt="">
  3.     <source srcset="images/thumbs/04.jpg" media="(min-width: 1280px)">
  4.     <source srcset="images/thumbs/05.jpg" media="(min-width: 980px)">
  5.     <source srcset="images/thumbs/06.jpg" media="(min-width: 320px)">
  6.     
  7.     <img src="data:image/jpeg;base64,/some_lqip_in_base_64==">
  8. </picture>
  9. ```

Example with video


  1. ``` html
  2. <video class="lozad" data-poster="images/backgrounds/video-poster.jpeg">
  3.     <source data-src="video/mov_bbb.mp4" type="video/mp4">
  4.     <source data-src="video/mov_bbb.ogg" type="video/ogg">
  5. </video>
  6. ```

Example with iframe


  1. ``` html
  2. <iframe data-src="embed.html" class="lozad"></iframe>
  3. ```
That's all, just add the lozad class.

Example toggling class


  1. ``` html
  2. <div data-toggle-class="active" class="lozad">
  3.     
  4. </div>
  5. ```
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 usinglozad.js

Contribute


Interested in contributing features and fixes?


Changelog


See the Changelog

License