Vanilla JavaScript plugin for lazyloading images

Overview

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/[email protected]/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.

Comments
  • Images not loading intermittently in Chromium browser when using elgg plugin

    Images not loading intermittently in Chromium browser when using elgg plugin

    i have been using this lazyload script as part of a plugin for the elgg framework: http://community.elgg.org/plugins/1122405/1.6/lazy-load-images

    i have been finding that on one particular type of content (site activity), the images do not always load in the chromium browser. usually if i refresh the page they will load on the 2nd time around.

    i have updated the jquery script to your latest version since the elgg plugin is 1+ year 'old' - which did improve the performance - yet this intermittent chromium issue remains.

    anyone have any idea what the cause of this may be?

    opened by propertunist 20
  • Fix: Initial scroll needed in chrome version 22.0.1229.94 to load imgs

    Fix: Initial scroll needed in chrome version 22.0.1229.94 to load imgs

    https://github.com/tuupola/jquery_lazyload/pull/48#  Was still happening for me. In Chrome version 22.0.1229.94 if page loaded and images were in view it would not load images until an initial scroll was made. I wrapped the initial update() call around a window.ready and it seems to have fixed it.

    opened by ItsMeAra 20
  • Using lazyload() breaks jQuery Mobile in iOS

    Using lazyload() breaks jQuery Mobile in iOS

    I set .lazyload() in $(document).bind('pageshow', function (){}) and jQuery Mobile doesn't follow links anymore. It works in Desktop browsers though, but on on the iOS.

    Bug 
    opened by firedev 17
  • Load images based on position from the top

    Load images based on position from the top

    Hi,

    I have images in 2 columns and in HTML these are 2 divs, so when I scroll the page instead of starting from the top it loads first column and then the second one. Is it possible to somehow load images in order?

    opened by firedev 14
  • Lazy Load is not working in Internet Explorer 8

    Lazy Load is not working in Internet Explorer 8

    "ajax-loader_s.gif" never gets replaced with "test.jpg", no JS errors shown in console.

    HTML:

    <img class="lazy" width="50" height="50" src="ajax-loader_s.gif" data-original="test.jpg" alt="" />
    

    JS:

    $(function() {
        $('img.lazy').lazyload();
    });
    
    opened by ZaDarkSide 13
  • Any scope for retina @x2 swap out?

    Any scope for retina @x2 swap out?

    https://github.com/neoziro/lazy-retina

    Was looking at this lazyload implementation and wondering if some sort of retina detection would be useful for this script as well?

    Would be ace to have the Retina support in one lazyload library rather than attempting to merge 2 in html source.

    Thoughts?

    Feature 
    opened by andrewminton 12
  • Lazyload and isotope

    Lazyload and isotope

    OK I'm building a new portfolio for myself at http://www.gablabelle.com/ and I'm using your lazyload plugin with isotope to showcase my images. It works great when I scroll down the page but it seems like the images don't show up when filtering without having previously scrolled down. If i scroll down nothing happens.

    It's acting like it doesn't consider the images to actually be in viewport (because of isotope).

    If I put a high number to failure_limit (like 100 or 1000) it will actually show the images after filtering if I scroll a bit.

    Is there something I can do to solve this?

    Many thanks.

    opened by gablabelle 11
  • Why isn't my simple code working?

    Why isn't my simple code working?

    Below is a very basic example, and images load before i scroll onto them. What possibly am i doing wrong?

    <!DOCTYPE html>
    <html>
    <head>
    	<title>Lazy loading</title>
    	                
    </head>
    <body>
    
    <img class="lazyload" data-src="img1.jpg">
    
    <img class="lazyload" data-src="img2.jpg">
    
    <img class="lazyload" data-src="img3.jpg">
    
    <img class="lazyload" data-src="img4.jpg">
    
    <img class="lazyload" data-src="img5.jpg">
    
    <script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/lazyload.js"></script>
    <script>
    	lazyload();
    </script>
    </body>
    </html>
    
    opened by prateekvarma 10
  • The 'appear' event and 'threshold' option - now there is not logic behaviour

    The 'appear' event and 'threshold' option - now there is not logic behaviour

    Hello!

    Now if i use the threshold option, the 'appear' event occurs for image which is not only in viewport area - even for image which is in threshold area behind screen. So now there is the constant order of events for image: 'appear' -> 'load'

    But i need and and i would like to suggest (i will make patch) a same behaviour:

    I think that the right behaviour should be as: the 'appear' event should occur for image when it occurs in viewport area first time only, but the 'load' event should occur when image was loaded (in viewport or not if we use the 'threshold' option). So there may be same event orders: 'appear' -> 'load' and 'load' -> 'appear' and even only 'load' without 'appear' event. I think it will be logical behaviour. And a user will be able to attach handler to 'appear' event (how he can do through plugin settings) and will catch it only when image in viewport area.

    What do you think about this?

    If you will say that it's nice suggestion - i will make a patch for this. I don't think that this feature will break a some old code of other application based in lazyload plugin because the order of events is not described detailed in documentation now.

    opened by Perlover 10
  • The first screen image cannot be loaded correctly

    The first screen image cannot be loaded correctly

    The first screen image cannot be loaded correctly

    If the HTML page layout is such:

    
    <div class="section" style="width:1000px;">
         <div class="article" style="width:500px;float:left;">
               <div class="text">text.....,More than one screen</div>
               <div class="img">
                  <img class="j-lazy" src="/images/t.gif" data-original="/images/test/2.jpg" width="250" height="160" />
                  <br/>
                  <img class="j-lazy" src="/images/t.gif" data-original="/images/test/2.jpg" width="250" height="160" />
               </div>
          </div>
          <div class="side" style="width:300px;float:right;">
              <img class="j-lazy" src="/images/t.gif" data-original="/images/test/2.jpg" width="250" height="160" />
          </div>
    </div>
    
    

    The left picture on second screen, while the right picture on first screen, the right screen images can't be loaded correctly.

    opened by kxbrand 10
  • Are Lazy Loaded Images Crawled By Google?

    Are Lazy Loaded Images Crawled By Google?

    Regarding the widely found issue on the web about dynamically loaded content that is / was not crawled by Google, my question is if Lazyload images are available for Google to crawl it and to show it for example on Google Images site (images.google.com).

    opened by ModDM 9
  • Demo link is not working

    Demo link is not working

    I found lazyload plugin in google search at https://plugins.jquery.com/lazyload/

    when i clicked on 'Try a Demo' link of above url page, it redirected me to below url page which does not exist.

    http://www.appelsiini.net/projects/lazyload/enabled_fadein.html

    Kindly fix the broken link. Thanks a lot.

    opened by kamleshwebtech 0
  • Does lazyload support <picture> source media?

    Does lazyload support source media?

    <picture> <source media="(max-width: 500px)" srcset="/img/1_small.png"> <img src="/images/1x1.png" class="lazyload" data-src="/img/1.jpg" style="width: 100%;" height="100%"> </picture>

    How to make 1_small.png lazyload?

    opened by eugenyshiryaev 2
  • WindowsWechat Not fully supported IntersectionObserver,isIntersecting attribute is undefined

    WindowsWechat Not fully supported IntersectionObserver,isIntersecting attribute is undefined

    ua:User-Agent,Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2785.116 Safari/537.36 QBCore/4.0.1295.400 QQBrowser/9.0.2524.400 Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/53.0.2875.116 Safari/537.36 NetType/WIFI MicroMessenger/7.0.5 WindowsWechat (Lazy Load)Version: 2.0.0-rc.2

    opened by gexin1 3
Releases(1.9.7)
Hemsida för personer i Sverige som kan och vill erbjuda boende till människor på flykt

Getting Started with Create React App This project was bootstrapped with Create React App. Available Scripts In the project directory, you can run: np

null 4 May 3, 2022
Kurs-repo för kursen Webbserver och Databaser

Webbserver och databaser This repository is meant for CME students to access exercises and codealongs that happen throughout the course. I hope you wi

null 14 Jan 3, 2023
Superlight vanilla javascript plugin, for modern web dropdowns. Supporting multi-options, search and images. Designed to be seamlessly themed

Superlight vanilla javascript dropdowns by LCweb Need to jump off of jQuery (or any other) dependency? Other packages are too heavy to just tweak sele

Luca 10 Dec 26, 2022
Simple jQuery plugin that will allow users to zoom in your images, perfect for product images and galleries.

Image Zoom (jQuery) Plugin Simple jQuery plugin that will allow users to zoom in your images, perfect for product images and galleries that is less th

Mario Duarte 8 Aug 3, 2022
Wallpik is a stock images & wallpapers downloading web app built with vanilla JavaScript & Pexels API

Wallpik the stock photos & wallpaper web app (Preview) Wallpik Wallpik is a single page application built with HTML, CSS, Vanilla JavaScript & Pexels

Shivaraj Padala 4 Apr 19, 2022
Compare James Webb Space Telescope images to older images.

How much more powerful is the James Webb Space Telescope when compared to Hubble? Find out! More info Want to help out? CONTRIBUTING.md Blog post with

John Christensen 399 Jan 3, 2023
Easiest 1-click way to install and use Stable Diffusion on your own computer. Provides a browser UI for generating images from text prompts and images. Just enter your text prompt, and see the generated image.

Stable Diffusion UI Easiest way to install and use Stable Diffusion on your own computer. No dependencies or technical knowledge required. 1-click ins

null 3.5k Dec 30, 2022
MidJourney is an AI text-to-image service that generates images based on textual prompts. It is often used to create artistic or imaginative images, and is available on Discord and through a web interface here.

Midjourney MidJourney is an AI text-to-image service that generates images based on textual prompts. It is often used to create artistic or imaginativ

Andrew Tsegaye 8 May 1, 2023
A vanilla js library to show preview images on hover

Hover Preview A vanilla js library to show preview images on hover, check a Demo here Please watch ?? or star ?? this repo if you like it. Getting sta

Atul Yadav 7 Dec 13, 2021
A JavaScript plugin for creating a tickerboard effect. Plugin for React or vanilla JS.

ticker-board A JavaScript plugin for creating a tickerboard effect. See the Ticker Board page for more info. Importing it There are basically two ways

Robin James Kerrison 6 Aug 11, 2022
This is a vanilla Node.js rest API created to show that it is possible to create a rest API using only vanilla Node.js

This is a vanilla Node.js rest API created to show that it is possible to create a rest API using only vanilla Node.js. But in most cases, I would recommend you to use something like Express in a production project for productivity purposes.

Eduardo Dantas 7 Jul 19, 2022
A markdown-it plugin that process images through the eleventy-img plugin. Can be used in any projects that uses markdown-it.

markdown-it-eleventy-img A markdown-it plugin that process images through the eleventy-img plugin. Can be used in any projects that use markdown-it. F

null 25 Dec 20, 2022
A JavaScript plugin to turn many images into a gif

GiffyImages A JavaScript plugin to turn many elements images into a gif magically. Preview Getting started npm i giffy-images -D or yarn add giffy-im

Leonardo Carey 6 Nov 6, 2021
A small, lightweight JavaScript plugin for placing items in straight rows (jQuery and vanilla JS version) – Demo:

rowGrid.js rowGrid.js is a small, lightweight (~1000 bytes gzipped) jQuery plugin for placing images (or other items) in straight rows. The grid is si

Bruno Joseph 669 Jul 22, 2022
Accordion Plugin written in Vanilla JavaScript.

Easy Accordion Accordion plugin written purely in JavaScript. Setup So, to start using the Easy Accordion plugin in your project, you can include the

Farhan Halai 1 Dec 16, 2020
Jspreadsheet is a lightweight vanilla javascript plugin to create amazing web-based interactive tables and spreadsheets compatible with other spreadsheet software.

Jspreadsheet CE v4: The JavaScript spreadsheet Jexcel CE has been renamed to Jspreadsheet CE News Important: Please import jspreadsheet.css (jexcel.cs

null 6.2k Dec 19, 2022
Vanilla Javascript plugin for manage kanban boards

jKanban Javascript plugin for Kanban boards jKanban allow you to create and manage Kanban Board in your project! Please try out the live demo! Install

Riccardo Tartaglia 898 Jan 3, 2023
Superlight vanilla javascript plugin improving forms look and functionality

Superlight pure javascript form switch plugin by LCweb Give a modern look to your applications and take advantage of javascript events and public func

Luca 31 Mar 9, 2022