Lazyload images, iframes or any src* element until they are visible in the viewport.

Overview

NOTE FROM MAINTAINERS This library is still working but not actively maintained, we recommend you use https://github.com/ApoorvSaxena/lozad.js first rather than this one. /NOTE FROM MAINTAINERS

lazyload Dependency Status devDependency Status

Lazyload images, iframes or any src* element until they are visible in the viewport.

Selenium Test Status

Install

npm install lazyloadjs --save

Also available for ">

>
<html>
  <head>
    <title>lazyloadtitle>
  head>
  <body>
    <script src="lazyload.min.js">script>

    
    

    <img
      data-src="real/image/src.jpg"
      src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="
      onload="lzld(this)">
  body>
html>

Make sure your webpage is in standards mode.

Viewport computing is badly handled by browsers when in quirksmode.

If you do not want to use a data-uri as your src, you can also use the provided b.gif which is the tiniest gif ever.

On most websites, you better let the first top images not bound to lzld method. So that they shows really fast.

Advanced example

lazyloadjs is an npm module and is compatible with browserify.

global.myLazyload = require('lazyloadjs')();
">
<img
  data-src="real/image/src.jpg"
  src="data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw=="
  onload="myLazyload(this)">

Per default, lazyloadjs exposes the lzld instance on the global object. So that in most cases, you just need to require it in your webpage.

API

var lzld = lazyload([opts])

opts is an object with these defaults:

{
  container: document.body,
  offset: 333,
  src: 'data-src' // or function(elt) { return customSrc }
}

opts.container is the referencing container, it's the viewport, defaults to document.body

opts.offset is a length in pixels used to compute when an element will soon be visible. So that you load it just before it becomes visible.

src is the attribute name storing the real src of the element to load.

src can also be a function, so that you can have your custom src computing algorithm. You can use it to lazyload High DPI/retina images.

Launching the examples

npm run examples

Developing

Launch the dev server:

npm run dev

Browse to http://localhost:8080/__zuul.

Tests are written with mocha.

Building

We provide a pre-built version of lazyloadjs in build/lazyload.min.js.

But you can build your own:

npm run build

You get the build in build/lazyload.min.js.

Please consider using browserify.

Sites using lazyload

Tens of millions of pageviews are served each month using this project:

.. And many unlisted websites, add yours!

Licence

Also see LICENCE.fasterize

(The MIT Licence)

Copyright (c) Vincent Voyer

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Comments
  • lazyload inside a specific container element

    lazyload inside a specific container element

    I have an element with overflow:auto set to it and lots of images inside it, in a list manner (one after the other) and I would like to apply lazyloading to it.

    opened by yairEO 23
  • lazy-retina

    lazy-retina

    Bonjour,

    Je suis développeur au Monde Intéractif, comme vous le savez nous utilisons votre lazyload sur notre site mobile (http://mobile.lemonde.fr), qui est d'ailleurs très performant.

    J'ai donc forké votre projet et j'y ai ajouté le support des écrans HD retina (https://github.com/neoziro/lazy-retina). Je reviens vers vous car le code peut vous intéresser et vous pourriez avoir envie de l'intégrer à votre script.

    Nous prévoyons très prochainement de déployer lazy-retina aussi sur la version desktop du site afin de supporter le nouvel iPad et le Macbook Pro Retina, et probablement d'autres devices prochainement.

    feature 
    opened by gregberge 10
  • Uglify config

    Uglify config

    Hi vvo ;)

    I'm surprised by the minification, which doesn't seem to be optimal.

    The built file is 4575 bytes, but I can get it to 3943 bytes by minifying it with another tool. Could it be a problem with the uglifyify params?

    (By the way, be careful with the screw-ie8 option!)

    opened by gmetais 9
  • Scrolling issue when using lazyload with FancyBox 2

    Scrolling issue when using lazyload with FancyBox 2

    Capture d e cran 2013-01-28 a 04 08 26 Certaines images, non visibles à l'écran, restent dans l'état "en cours de chargement" (c'est à dire que l'attribut src n'est pas remplacé par la valeur réelle de l'image) alors que ces images sont accessibles de façon "classique"

    Contexte : Quelque soit le navigateur utilisé (à l'exception d'ie car sous mac os x). Ces images sont contenues dans une popup utilisant Fancybox 2 (en mode html pas iframe ou autre).

    Ps : Merci pour ce petit framework bien pratique :)

    opened by Krossnine 8
  • Scroll on ipad doesn't seems to fire onload event until the pressure is released

    Scroll on ipad doesn't seems to fire onload event until the pressure is released

    Hi @vvo,

    On ipad, we can see a bad lazy loading because it seems that until the pressure on the screen is not release (during the scrolling), no images is shown.

    To reproduce the behavior, you need to go to your demo site on an ipad (safari or chrome) and scroll slowly and look at what happens when you release the pressure.

    I try to understand what's going on but that's not obvious. The infos I have for the moment :+1: http://stackoverflow.com/questions/20318002/animate-on-scroll-in-mobile-safari http://stackoverflow.com/questions/10482227/javascript-dom-changes-in-touchmove-delayed-until-scroll-ends-on-mobile-safari/10856671#10856671 http://api.jquerymobile.com/scrollstart/

    opened by abarre 7
  • Small bug with images partially visible

    Small bug with images partially visible

    I have a small bug detected in responsive case. Here is the example:

    a 1400x400px banner, centered on the browser.

    On mobile (let's say width = 500px), the top-left pixel position is top: 0, left: (500 - 1400)/2 = -450px As 450 > the base offset, the image is considered as not inViewport, but as the image width is 1400, it should be considered "in".

    Thank you

    opened by jdeniau 7
  • The lazyload doesn't work on chrome v31

    The lazyload doesn't work on chrome v31

    Hi @vvo,

    We saw today that the lazyload doesn't work on chrome 31. We see the problem on mac and linux. It's working fine on firefox.

    If you go on the demo page http://vvo.github.io/lazyload/, you will see that all the images are loaded at the beginning.

    opened by abarre 5
  • Lazyload inside a long div

    Lazyload inside a long div

    I want to lazyload in a scrollable div container that is quite long. So i made a <script> at the end of the div to declare my own instance of lazyload attached to this container.

    Problem: Chrome (and Safari) will sometime launch the "onload" event of my images before the div is closed.

    Here is a fiddle : http://jsfiddle.net/GJ8Cb/2/ (on Chrome and Safari, but does not reproduce every time)

    opened by gmetais 5
  • Pictures are not loaded in position: absolute popup with an overflow

    Pictures are not loaded in position: absolute popup with an overflow

    I've got a popup which containing a list of pictures. This popup is in absolute positionning with an overflow:hidden. The pictures in the viewport are instantly loaded but not the outside ones. When i scroll in the popup hidden pictures are not loaded. When i scroll the page under the popup, they suddenly load. Is there a way to attach an event when i scroll the popup to fire lzld()?

    opened by adrianpoix 5
  • Change minifying engine to closure compiler

    Change minifying engine to closure compiler

    Closure compiler complains a little during the build.

    WARNING - If this if/for/while really shouldn't have a body, use {}
    

    This empty loop is the reason:

    for (var i = this.length; i-- && this[i] !== value;) {}
    

    But it works!

    opened by gmetais 4
  • Browser compatibility list

    Browser compatibility list

    Hi,

    i'm an happy user of https://github.com/fasterize/lazyload, but i want more (iframe lazy loading) What old browser are you supporting? (it would be great to state it in readme)(I only need IE8+)

    opened by echampet 4
  • Update README.md

    Update README.md

    I'm requesting again and again to update the readme with my website Better Gift Flowers but you're deleting it. I would like to know whats the problem?

    opened by BetterGift 1
  • Support for WebP images with fallback to jpeg/png

    Support for WebP images with fallback to jpeg/png

    With increasing popularity of webp images, its essential to have support for lazyloading these images. But because webp image format is not supported by all browsers, it is essential to add fallback support to jpeg/png versions in lazyloading itself.

    opened by abhishek9 0
  • Usability with webpack ?

    Usability with webpack ?

    Hi,

    I'm trying to switch to webpack a big project using lazyload but as the script needs to be loaded on the top on the page, I do not manage to make it work.

    The config looks like this:

    module.exports = {
      entry: {
        head: [
          './custom-lazyload.js', // which does `import lazyload from 'lazyloadjs'`
        ],
        global: [
          // some files
        ]
      },
      plugins: [
        new webpack.optimize.CommonsChunkPlugin({
          name: 'vendor',
        })
      ]
    }
    

    I got tree generated files: head.js, global.js and vendor.js but the webpack "bootstrap" is in the vendor and the vendor is huge, I do not want to load it on the top of the page.

    The head.js start like this: webpackJsonp([16],{ and so the webpackJsonp function does not exists.

    Do you have by any change a working example with webpack ? or an idea on how to make it work ?

    Thank you

    opened by jdeniau 1
  • embed tag

    embed tag

    Thank you for this great script.

    I look forward to the possibility of lazy loading on div background . I could carry on my side by adding an image below <img> that has the role of background . But the code is not very beautiful.

    I just used your script on embed tags for flash files . This works but you have to know two things : onload does not work on embed tag and Chrome does not support the change src ( you have to refresh the parent content)

    I achieved this by adding an ID to the embed and via the function:

    function lzldEmbed(e) { e = document.getElementById(e); var s = setInterval(function(){ if (e.src!='') { var c = e.parentElement; c.innerHTML = c.innerHTML; clearInterval(s); } },100); lzld(e); }

    unconfirmed 
    opened by marin-df 1
zkPoB is a mobile compatible tool that lets anyone prove they own a Bufficorn (or any NFT) without revealing which Buffi they own or the address they are verifying themselves with

zkPoB is a mobile compatible tool that lets anyone prove they own a Bufficorn (or any NFT) without revealing which Buffi they own or the address they are verifying themselves with

Marto.eth 10 Aug 25, 2022
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.

lazysizes lazysizes is a fast (jank-free), SEO-friendly and self-initializing lazyloader for images (including responsive images picture/srcset), ifra

Alexander Farkas 16.6k Jan 1, 2023
adds the *scrollin* and *scrollout* events to jquery, which will fire when any given element becomes (respectively) visible and invisible in the browser viewpori

jQuery.scrolling This plugin adds the scrollin and scrollout events to jquery: these events will fire when any given element becomes visible/invisible

Dark 5 Apr 7, 2021
Tenzi is a dice game. The player needs to roll dice until they are all the same. Clicking on a dice, freezes it at its current value between rolls. Best scores are saved to local storage.

Roll until all dice are the same Try me! Technologies Used Description Tenzi is a dice game used to demonstrate the use of React Hooks (useState, useE

Michael Kolesidis 7 Nov 23, 2022
The world's fastest game! User gets ten dice. Then rolls and rolls as fast as they can until he gets all their dice on the same number.

tenzies-game The world's fastest game! User gets ten dice. Then rolls and rolls as fast as they can until he gets all their dice on the same number. I

Babur Shah Sayer 6 Nov 26, 2022
🎤Rickroll in style - This uses a GitHub action that runs a NodeJS script, found in src.

This uses a GitHub action that runs a NodeJS script, found in src. This then commits the finished HTML to index.html, and then GitHub Pages will build that HTML file, allowing you to rickroll your friends.

Blue 5 Sep 25, 2022
BttrLazyLoading is a Jquery plugin that allows your web application to defer image loading until images are scrolled to but not only

BttrLazyLoading.js BttrLazyLoading is a Jquery plugin that allows your web application to defer image loading until images are scrolled to but not onl

Julien Renaux 410 Dec 14, 2022
Calculating Pi number without limitation until 10k digits or more in your browser powered by JS without any third party library!

PI Calculator Web JS (Online) Calculating Pi number without limitation until 10k digits or more in your browser powered by JS without any third party

Max Base 6 Jul 27, 2022
An Obsidian plugin that allows adding iframes with custom styling as editor tabs. Also comes with presets for Google Keep and more.

Obsidian Custom Frames An Obsidian plugin that turns web apps into panes using iframes with custom styling. Also comes with presets for Google Keep an

Ellpeck 208 Jan 8, 2023
A superfast and easy to use knowledge base to help your customers get the info they need, when they need it most.

A superfast and easy to use knowledge base to help your customers get the info they need, when they need it most. helpkb is an open-source Next.js (A

Mark Moffat 11 Dec 5, 2022
A tiny JavaScript library to easily toggle the state of any HTML element in any contexts, and create UI components in no time.

A tiny JavaScript library to easily toggle the state of any HTML element in any contexts, and create UI components in no time. Dropdown, navigation bu

Matthieu Bué 277 Nov 25, 2022
Given a list of items, only render what's visible on the screen while allowing scrolling the whole list.

Solid Windowed Given a list of items, only render what's visible on the screen while allowing scrolling the whole list. A Solid component. See https:/

Tito 40 Dec 21, 2022
Chrome extension that applies phrase-based line breaking and visible phrase boundaries to the current page.

BudouX Chrome Extension This extension applies the phrase-based line breaking or the Japanese Wakachi-gaki style line breaking to the current page. Pl

Google 9 Nov 18, 2022
Catalogist is the easy way to catalog and make your software and (micro)services visible to your organization in a lightweight and developer-friendly way.

catalogist ?? ?? ?? ?? ?? The easy way to catalog and make your software and (micro)services visible to your organization through an API You were a pe

Mikael Vesavuori 11 Dec 13, 2022
A ScrollSpy library for detecting enter/exit of elements in the viewport when the user scrolls

jquery-scrollspy A jQuery plugin for detecting enter/exit of elements in the viewport when the user scrolls. New Features Added a couple new features:

John Smart 276 Jul 1, 2022
Device.js is a JavaScript library to detect device, viewport, and browser information using plain JavaScript.

Device.js Device.js is a JavaScript library to detect device, viewport, and browser information using plain JavaScript. Compatibility Works with all m

Emanuel R. Vásquez 5 Dec 16, 2022
A alternative to Github Copilot for vscode until you get the access to github copilot

Clara-Copilot ?? Code Howdy ?? I know you guys are ?? waiting for Github Copilot Here is a alternative one for now ?? ⭐ Star is all i want nothing muc

BadBoy17G 245 Jan 8, 2023
A simple extension that shows how many days a user has left until the date of the deadline

A simple extension that shows how many days a user has left until the date of the deadline

Mohamed Haddi 10 Nov 18, 2022
JavaScript micro-library: pass in an element and a callback and this will trigger when you click anywhere other than the element

Add a click listener to fire a callback for everywhere on the window except your chosen element. Installation run npm install @lukeboyle/when-clicked-

Boyleing Point 5 May 13, 2021