Convert Figma logo to code with AI

tuupola logolazyload

Vanilla JavaScript plugin for lazyloading images

8,758
2,237
8,758
109

Top Related Projects

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.

17,454

High performance and SEO friendly lazy loader for images (responsive and normal), iframes and more, that detects any visibility changes triggered through user interaction, CSS or JavaScript without configuration.

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

1,366

A fast, flexible, and small SEO-friendly lazy loader.

2,626

Hey, be lazy! bLazy.JS is a lightweight pure JavaScript script for lazy loading and multi-serving images. It's working in all modern browsers including IE7+.

Quick Overview

Tuupola/lazyload is a lightweight JavaScript library for lazy loading images and other elements on web pages. It defers loading of images, iframes, and videos until they enter the viewport, improving initial page load times and reducing bandwidth usage.

Pros

  • Easy to implement with minimal configuration required
  • Supports modern browsers and has fallbacks for older ones
  • Lightweight and performant, with no dependencies
  • Customizable with various options and callbacks

Cons

  • May not be suitable for complex lazy loading scenarios
  • Limited documentation compared to some larger libraries
  • Requires JavaScript to function, which may not be ideal for all use cases
  • Less active development compared to some alternatives

Code Examples

  1. Basic usage for lazy loading images:
<img class="lazyload" data-src="image.jpg" width="220" height="280">
<script src="lazyload.min.js"></script>
<script>
  new LazyLoad();
</script>
  1. Lazy loading with custom options:
const lazyLoadInstance = new LazyLoad({
  elements_selector: ".lazy",
  threshold: 0,
  callback_enter: (element) => {
    console.log("Element entered viewport");
  }
});
  1. Lazy loading iframes:
<iframe class="lazyload" data-src="https://www.youtube.com/embed/dQw4w9WgXcQ" frameborder="0" allowfullscreen></iframe>
<script>
  new LazyLoad({
    elements_selector: "iframe"
  });
</script>

Getting Started

  1. Include the lazyload.min.js script in your HTML:
<script src="https://cdn.jsdelivr.net/npm/lazyload@2.0.0-rc.2/lazyload.min.js"></script>
  1. Add the lazyload class and data-src attribute to your images:
<img class="lazyload" data-src="image.jpg" width="220" height="280">
  1. Initialize LazyLoad in your JavaScript:
document.addEventListener("DOMContentLoaded", function() {
  new LazyLoad();
});

Competitor Comparisons

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.

Pros of vanilla-lazyload

  • More actively maintained with frequent updates
  • Supports native lazy loading for modern browsers
  • Offers more customization options and features

Cons of vanilla-lazyload

  • Slightly larger file size
  • May have a steeper learning curve due to more configuration options

Code Comparison

lazyload:

var myLazyLoad = new LazyLoad({
    elements_selector: ".lazy"
});

vanilla-lazyload:

var lazyLoadInstance = new LazyLoad({
    elements_selector: ".lazy",
    use_native: true,
    threshold: 300
});

Both libraries use similar syntax for basic implementation, but vanilla-lazyload offers more configuration options out of the box, such as native lazy loading support and custom threshold settings.

vanilla-lazyload provides a more feature-rich and actively maintained solution, while lazyload offers a simpler, lightweight alternative. The choice between the two depends on specific project requirements, browser support needs, and desired customization level.

17,454

High performance and SEO friendly lazy loader for images (responsive and normal), iframes and more, that detects any visibility changes triggered through user interaction, CSS or JavaScript without configuration.

Pros of lazysizes

  • More feature-rich, supporting responsive images, AJAX loading, and plugins
  • Better performance with native lazy loading support and automatic unveiling
  • Larger community and more frequent updates

Cons of lazysizes

  • Slightly larger file size due to additional features
  • May be overkill for simple lazy loading needs
  • Steeper learning curve for advanced features

Code Comparison

lazysizes:

<img data-src="image.jpg" class="lazyload" alt="Lazy loaded image">
<script src="lazysizes.min.js" async=""></script>

lazyload:

<img class="lazyload" data-src="image.jpg" alt="Lazy loaded image">
<script>
  new LazyLoad();
</script>

Both libraries use similar markup for lazy loading images, with the main difference being the initialization process. lazysizes automatically initializes itself, while lazyload requires manual instantiation.

lazysizes offers more advanced features and better performance, making it suitable for complex projects. However, lazyload is simpler and may be preferable for basic lazy loading needs. The choice between the two depends on the project's requirements and complexity.

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

Pros of lozad.js

  • Smaller file size (1.9KB gzipped) compared to lazyload's larger footprint
  • Uses Intersection Observer API for better performance and efficiency
  • Supports lazy loading of background images and videos

Cons of lozad.js

  • Less browser support due to reliance on Intersection Observer API
  • Fewer customization options compared to lazyload's extensive configuration
  • Limited documentation and examples compared to lazyload's comprehensive guides

Code Comparison

lozad.js:

const observer = lozad('.lozad', {
    rootMargin: '10px 0px',
    threshold: 0.1
});
observer.observe();

lazyload:

lazyload(document.querySelectorAll(".lazyload"), {
    root: null,
    rootMargin: "0px",
    threshold: 0
});

Both libraries offer simple implementation, but lozad.js uses a more modern approach with the Intersection Observer API. lazyload provides more options for customization and fallback methods for older browsers.

lozad.js is ideal for projects targeting modern browsers and prioritizing performance, while lazyload offers broader compatibility and more features at the cost of a larger file size.

1,366

A fast, flexible, and small SEO-friendly lazy loader.

Pros of yall.js

  • Smaller file size (2.3KB gzipped) compared to lazyload's 3.5KB
  • Supports native lazy loading with fallback for older browsers
  • Includes IntersectionObserver polyfill for broader compatibility

Cons of yall.js

  • Less customization options than lazyload
  • Fewer stars and contributors on GitHub, potentially indicating less community support
  • Limited documentation compared to lazyload's more comprehensive guides

Code Comparison

lazyload:

lazyload();

yall.js:

document.addEventListener("DOMContentLoaded", yall);

Both libraries offer simple initialization, but yall.js requires explicit event listener setup.

Feature Comparison

  • lazyload supports more media types (images, videos, iframes)
  • yall.js focuses primarily on images and background images
  • Both support responsive images and placeholders
  • lazyload offers more advanced callback functions

Performance

Both libraries are lightweight and performant, but yall.js has a slight edge in file size and native lazy loading support, potentially offering better performance on modern browsers.

Community and Maintenance

lazyload has a larger user base and more frequent updates, while yall.js has fewer contributors but is still actively maintained.

2,626

Hey, be lazy! bLazy.JS is a lightweight pure JavaScript script for lazy loading and multi-serving images. It's working in all modern browsers including IE7+.

Pros of blazy

  • Smaller file size (2KB minified and gzipped) compared to lazyload's larger footprint
  • Supports more browsers, including older versions of Internet Explorer
  • Offers a CDN option for easy integration

Cons of blazy

  • Less frequent updates and maintenance compared to lazyload
  • Fewer configuration options and customization features
  • Limited documentation and examples compared to lazyload's comprehensive guides

Code Comparison

blazy:

var bLazy = new Blazy({
    selector: 'img',
    offset: 100,
    success: function(element){
        console.log("Element loaded");
    }
});

lazyload:

var lazyLoadInstance = new LazyLoad({
    elements_selector: "img",
    threshold: 100,
    callback_loaded: function(element) {
        console.log("Element loaded");
    }
});

Both libraries offer similar basic functionality for lazy loading images, with slight differences in syntax and naming conventions. blazy uses offset while lazyload uses threshold for setting the distance at which images start loading. The callback function for successful loading is named differently in each library.

Overall, blazy is more lightweight and supports older browsers, while lazyload offers more features and better documentation. The choice between the two depends on specific project requirements and browser support needs.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Lazy Load Remastered

Lazy Load delays loading of images in long web pages. Images outside of viewport will not be loaded before user scrolls to them. This is opposite of image preloading.

This is a modern vanilla JavaScript version of the original Lazy Load plugin. It uses Intersection Observer API to observe when the image enters the browsers viewport. Original code was inspired by YUI ImageLoader utility by Matt Mlinac. New version loans heavily from a blog post by Dean Hume.

Basic Usage

By default Lazy Load assumes the URL of the original high resolution image can be found in data-src attribute. You can also include an optional low resolution placeholder in the src attribute.

<script src="https://cdn.jsdelivr.net/npm/lazyload@2.0.0-rc.2/lazyload.js"></script>

<img class="lazyload" data-src="img/example.jpg" width="765" height="574" />
<img class="lazyload" src="img/example-thumb.jpg" data-src="img/example.jpg" width="765" height="574" />

With the HTML in place you can then initialize the plugin using the factory method. If you do not pass any settings or image elements it will lazyload all images with class lazyload.

lazyload();

If you prefer you can explicitly pass the image elements to the factory. Use this for example if you use different class name.

let images = document.querySelectorAll(".branwdo");
lazyload(images);

If you prefer you can also use the plain old constructor.

let images = document.querySelectorAll(".branwdo");
new LazyLoad(images);

The core IntersectionObserver can be configured by passing an additional argument

new LazyLoad(images, {
     root: null,
     rootMargin: "0px",
     threshold: 0
});

Additional API

To use the additional API you need to assign the plugin instance to a variable.

let lazy = lazyload();

To force loading of all images use loadimages().

lazy->loadImages();

To destroy the plugin and stop lazyloading use destroy().

lazy->destroy();

Note that destroy() does not load the out of viewport images. If you also want to load the images use loadAndDestroy().

lazy->loadAndDestroy();

Additional API is not avalaible with the jQuery wrapper.

jQuery Wrapper

If you use jQuery there is a wrapper which you can use with the familiar old syntax. Note that to provide BC it uses data-original by default. This should be a drop in replacement for the previous version of the plugin.

<img class="lazyload" data-original="img/example.jpg" width="765" height="574">
<img class="lazyload" src="img/example-thumb.jpg" data-original="img/example.jpg" width="765" height="574">
$("img.lazyload").lazyload();

Cookbook

Blur Up Images

Low resolution placeholder ie. the "blur up" technique. You can see this in action in this blog entry. Scroll down to see blur up images.

<img class="lazyload"
     src="thumbnail.jpg"
     data-src="original.jpg"
     width="1024" height="768" />
<div class="lazyload"
     style="background-image: url('thumbnail.jpg')"
     data-src="original.jpg" />

Responsive Images

Lazyloaded Responsive images are supported via data-srcset. If browser does not support srcset and there is no polyfill the image from data-src will be shown.

<img class="lazyload"
     src="thumbnail.jpg"
     data-src="large.jpg"
     data-srcset="small.jpg 480w, medium.jpg 640w, large.jpg 1024w"
     width="1024" height="768" />
<img class="lazyload"
     src="thumbnail.jpg"
     data-src="normal.jpg"
     data-srcset="normal.jpg 1x, retina.jpg 2x"
     width="1024" height="768" />

Inlined Placeholder Image

To reduce the amount of request you can use data uri images as the placeholder.

<img src="data:image/gif;base64,R0lGODdhAQABAPAAAMPDwwAAACwAAAAAAQABAAACAkQBADs="
     data-src="original.jpg"
     width="1024" height="768" />

Install

This is still work in progress. You can install beta version with yarn or npm.

$ yarn add lazyload
$ npm install lazyload

License

All code licensed under the MIT License. All images licensed under Creative Commons Attribution 3.0 Unported License. In other words you are basically free to do whatever you want. Just don't remove my name from the source.

NPM DownloadsLast 30 Days