GithubHelp home page GithubHelp logo

guardwu2015 / lozad.js Goto Github PK

View Code? Open in Web Editor NEW

This project forked from apoorvsaxena/lozad.js

0.0 1.0 0.0 1.28 MB

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

Home Page: https://apoorv.pro/lozad.js/demo/

License: MIT License

JavaScript 100.00%

lozad.js's Introduction

Lozad.js npm version Build Status

Highly performant, light ~0.7kb 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 responsive images and background images

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:

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 custom options:

const observer = lozad('.lozad', {
    rootMargin: '10px 0px', // syntax similar to that of CSS Margin
    threshold: 0.1 // ratio of element convergence
});
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 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>

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

Browser Support

Available in latest browsers. If browser support is not available, then make use of this polyfill.

FAQs

Checkout the FAQ Wiki for some common gotchas to be aware of while using lozad.js

Sites using Lozad.js

Support Lozad.js

Lozad.js is completely free and open-source. If you find it useful, you can show your support by starring it or sharing it in your social network or by simply letting me know how much you 💖 it by tweeting to @Apoorv_Saxena.

Contribute

Interested in contributing features and fixes?

Read more on contributing.

Changelog

See the Changelog

License

MIT © Apoorv Saxena

lozad.js's People

Contributors

apoorvsaxena avatar thiamsantos avatar lptn avatar stephenkingsley avatar fregante avatar lionralfs avatar zabawaba99 avatar

Watchers

Wuyue avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.