All Projects → ApoorvSaxena → Lozad.js

ApoorvSaxena / Lozad.js

Licence: mit
🔥 Highly performant, light ~1kb and configurable lazy loader in pure JS with no dependencies for responsive images, iframes and more

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Lozad.js

Yall.js
A fast, flexible, and small SEO-friendly lazy loader.
Stars: ✭ 1,163 (-83.22%)
Mutual labels:  images, performance, lazy-loading, lazyload
Jquery.lazy
A lightweight, fast, feature-rich, powerful and highly configurable delayed content, image and background lazy loading plugin for jQuery & Zepto.
Stars: ✭ 965 (-86.08%)
Mutual labels:  images, lazy-loading, lazyload
Accessible Image Lazy Load
😴 gandul! accessible lazy loading images
Stars: ✭ 281 (-95.95%)
Mutual labels:  images, lazy-loading, lazyload
Vanilla Lazyload
LazyLoad is a lightweight, flexible script that speeds up your website by deferring the loading of your below-the-fold images, backgrounds, videos, iframes and scripts to when they will enter the viewport. Written in plain "vanilla" JavaScript, it leverages IntersectionObserver, supports responsive images and enables native lazy loading.
Stars: ✭ 6,596 (-4.85%)
Mutual labels:  images, lazy-loading, lazyload
Wipe Modules
🗑️ Easily remove the node_modules folder of non-active projects
Stars: ✭ 304 (-95.61%)
Mutual labels:  hacktoberfest, performance
Hint
💡 A hinting engine for the web
Stars: ✭ 3,280 (-52.68%)
Mutual labels:  hacktoberfest, performance
Ava
Node.js test runner that lets you develop with confidence 🚀
Stars: ✭ 19,458 (+180.7%)
Mutual labels:  hacktoberfest, performance
Bigcache
Efficient cache for gigabytes of data written in Go.
Stars: ✭ 5,304 (-23.49%)
Mutual labels:  hacktoberfest, performance
Yii2 Imagine
Yii 2 imagine extension
Stars: ✭ 271 (-96.09%)
Mutual labels:  hacktoberfest, images
Guider
Performance Analyzer
Stars: ✭ 393 (-94.33%)
Mutual labels:  hacktoberfest, performance
React Esi
React ESI: Blazing-fast Server-Side Rendering for React and Next.js
Stars: ✭ 537 (-92.25%)
Mutual labels:  hacktoberfest, performance
Kubectl Flame
Kubectl plugin for effortless profiling on kubernetes
Stars: ✭ 297 (-95.72%)
Mutual labels:  hacktoberfest, performance
Sqip
"SQIP" (pronounced \skwɪb\ like the non-magical folk of magical descent) is a SVG-based LQIP technique.
Stars: ✭ 3,074 (-55.65%)
Mutual labels:  images, performance
Predator
A powerful open-source platform for load testing APIs.
Stars: ✭ 356 (-94.86%)
Mutual labels:  hacktoberfest, performance
React Lazy Load Image Component
React Component to lazy load images and components using a HOC to track window scroll position.
Stars: ✭ 755 (-89.11%)
Mutual labels:  lazy-loading, lazyload
Laravel Medialibrary
Associate files with Eloquent models
Stars: ✭ 4,743 (-31.58%)
Mutual labels:  hacktoberfest, images
Sinuous
🧬 Light, fast, reactive UI library
Stars: ✭ 740 (-89.32%)
Mutual labels:  hacktoberfest, performance
Kroki
Creates diagrams from textual descriptions!
Stars: ✭ 774 (-88.83%)
Mutual labels:  hacktoberfest, images
jekyll-loading-lazy
🧙🏽‍♀️ Automatically adds loading="lazy" to <img> and <iframe> tags. Load images on your sites lazily without JavaScript.
Stars: ✭ 41 (-99.41%)
Mutual labels:  lazyload, lazy-loading
magento2-catalog-lazy-load
Improve the load time of your Magento 2 categories pages by loading your images on demand with our Lazy Load Extension
Stars: ✭ 56 (-99.19%)
Mutual labels:  lazyload, lazy-loading

Lozad.js npm version Build Status npm

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 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

# 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
import lozad from 'lozad'

// using CommonJS modules
var lozad = require('lozad')

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

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

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):

<img class="lozad" data-src="image.png">

All you need to do now is just instantiate Lozad as follows:

const observer = lozad(); // lazy loads elements with default selector as '.lozad'
observer.observe();

or with a DOM Element reference:

const el = document.querySelector('img');
const observer = lozad(el); // passing a `NodeList` (e.g. `document.querySelectorAll()`) is also valid
observer.observe();

or with custom options:

const observer = lozad('.lozad', {
    rootMargin: '10px 0px', // syntax similar to that of CSS Margin
    threshold: 0.1, // ratio of element convergence
    enableAutoReload: true // it will reload the new image when validating attributes changes
});
observer.observe();

Reference:

or if you want to give custom function definition to load element:

lozad('.lozad', {
    load: function(el) {
        console.log('loading element');

        // Custom implementation to load an element
        // e.g. el.src = el.getAttribute('data-src');
    }
});

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.

lozad('.lozad', {
    loaded: function(el) {
        // Custom implementation on a loaded element
        el.classList.add('loaded');
    }
});

If you want to lazy load dynamically added elements:

const observer = lozad();
observer.observe();

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

for use with responsive images

<!-- responsive image example -->
<img class="lozad" data-src="image.png" data-srcset="image.png 1000w, image-2x.png 2000w">

for use with background images

<!-- background image example -->
<div class="lozad" data-background-image="image.png">
</div>

for use with multiple background images

<!-- multiple background image example -->
<div class="lozad" data-background-image="path/to/first/image,path/to/second/image,path/to/third/image">
</div>

for use with responsive background images (image-set)

<!-- responsive background image-set example -->
<div class="lozad" data-background-image-set="url('photo.jpg') 1x, url('[email protected]') 2x">
</div>

To change the delimiter that splits background images:

<!-- custom delimiter for background images example -->
<div
  class="lozad"
  data-background-image="/first/custom,image,path/image.png-/second/custom,image,path/image.png"
  data-background-delimiter="-"
>
</div>

If you want to load the images before they appear:

const observer = lozad();
observer.observe();

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

Large image improvment

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

<img class="lozad" data-placeholder-background="red" data-src="image.png">

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

<!-- 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 -->
<picture class="lozad" style="display: block; min-height: 1rem" data-iesrc="images/thumbs/04.jpg" data-alt="">
    <source srcset="images/thumbs/04.jpg" media="(min-width: 1280px)">
    <source srcset="images/thumbs/05.jpg" media="(min-width: 980px)">
    <source srcset="images/thumbs/06.jpg" media="(min-width: 320px)">
    <!-- 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> -->
</picture>

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.

<picture class="lozad" style="display: block; min-height: 1rem" data-iesrc="images/thumbs/04.jpg" data-alt="">
    <source srcset="images/thumbs/04.jpg" media="(min-width: 1280px)">
    <source srcset="images/thumbs/05.jpg" media="(min-width: 980px)">
    <source srcset="images/thumbs/06.jpg" media="(min-width: 320px)">
    <!-- you can define a low quality image placeholder that will be removed when the picture is loaded -->
    <img src="data:image/jpeg;base64,/some_lqip_in_base_64==">
</picture>

Example with video

<video class="lozad" data-poster="images/backgrounds/video-poster.jpeg">
    <source data-src="video/mov_bbb.mp4" type="video/mp4">
    <source data-src="video/mov_bbb.ogg" type="video/ogg">
</video>

Example with iframe

<iframe data-src="embed.html" class="lozad"></iframe>

That's all, just add the lozad class.

Example toggling class

<div data-toggle-class="active" class="lozad">
    <!-- content -->
</div>

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?

Read more on contributing.

Changelog

See the Changelog

License

MIT © Apoorv Saxena

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].