Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

lozad

Package Overview
Dependencies
Maintainers
1
Versions
24
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lozad

A light-weight JS library to lazy load any HTML element such as images, ads, videos etc.

  • 0.0.3
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
31K
increased by0.56%
Maintainers
1
Weekly downloads
 
Created
Source

Lozad npm version

Advanced performant Lazy Loader using Intersection Observer API


lozad.js lazy loading javascript library

  • is a light-weight library, just 569 bytes minified & gzipped,
  • has NO DEPENDENCIES :)
  • lazy loads elements performantly using pure JavaScript.

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

Yet another Lazy Loading JavaScript library, why?

Existing lazy loading libraries hook up 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.

Demo

Get Started

Get the library using one of the following ways:

  1. GitHub

Full build

  1. Yarn : yarn add lozad

  2. Bower : bower install lozad

  3. npm: npm install --save lozad

  4. CDN: https://cdn.jsdelivr.net/npm/lozad

Now include the library in the HEAD tag of your page:

<script type="text/javascript" src="lozad.js"></script>

or

<script type="text/javascript" src="lozad.min.js"></script>

Note: lozad.js supports AMD and commonJS module pattern out of the box.

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:

new Lozad(); // lazy loads images with selector as '.lozad'

or with custom options:

new Lozad({
    selector: '.lozad', // for identification of images to lazy load
    rootMargin: '10px 0px', // syntax similar to that of CSS Margin
    threshold: 0.1 // ratio of image convergence
})

Reference:

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

var lozad = new Lozad();

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

	// Custom implementation to load an element
	// e.g. el.src = el.dataset.src;
}

If you want to lazy load dynamically added images:

var lozad = new Lozad();

// ... code to dynamically add elements
lozad.activate(); // observes newly added elements as well

Browser Support

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

Contributing

Interested in contributing features and fixes?

Read more on contributing.

Changelog

See the Changelog

License

Copyright (c) 2017 Apoorv Saxena, https://apoorv.pro Licensed under the MIT license.

Keywords

FAQs

Package last updated on 05 Sep 2017

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc