GithubHelp home page GithubHelp logo

lazyload's Introduction

Lazyload

An image lazyloader is designed to save requests on page loading (#webperf)

Most of the time, when you have 100 images on a page, your user doesn't need them all.

This lazyloader will only load what is necessary.

It's a standalone script that weights 1398 bytes minified gzipped.

How to use

<!doctype html>
<html>
  <head>
    <title></title>
    <!-- in your concatenated bundle, inlined, alone and external -->
    <script src="lazyload.min.js"></script>
    <script>
      // For best results, initialize now
      window.lzld = (new Lazyload).lzld;
    </script>
    <!-- your scripts goes here -->
  </head>
  <body>
    <img
      data-src="real/image/src.jpg"
      src=data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
      onload=lzld(this) onerror=lzld(this) />  
  </body>
</html>

Features

  • Cut onload time, page size, number of request by 2x, 3x? Depends on your website
  • Horizontal and vertical (scroll) lazy loading of images
  • Custom container (default to document.body)
  • Many edge cases covered (like slow domready)
  • Has tests/
  • Production ready: YES. Used on major websites like lemonde, rue89, playtv, voyages-sncf

Why another lazyload plugin

We could not find any standalone lazyloader but the one on stackoverflow.

We first used that one, then we re-wrote it entirely with ideas from mod_pagespeed lazyloader.

We're now upgrading our lazyload from time to time to make it more robust.

Browser support

IE6+ or modern browser.

IE6/7 originally does not support data uri:s images but using the onerror event on to-be-lazyloaded images, we're able to register the current image in the lazyloader. The only drawback is that you can have red crosses showing that original data uri:s image cannot be loaded. But well, it's old IE so no big deal.

You can have IE6/7 support without the hack, use the b.gif image instead of the data uri:s and remove onerror.

How does it works

We watch the domready event.

But if it takes too much time to fire, we use the <img onload=lzld(this) fallback that will fire before the domready event.

Scroll and resize events are throttled so that we do not run too often.

Adding to the <head> is mandatory otherwise we could not show images as fast as we want. And we would not be the first script to register to the domready event.

The base 64 src should be the smallest possible. Reference: http://probablyprogramming.com/2009/03/15/the-tiniest-gif-ever

Don't worry about the size overhead of adding a lot of base 64 src images to your page : GZIP is here to help (http://www.gzip.org/deflate.html).

CMS integration

Automate

If you want to automatically add lazyload to your website, contact http://fasterize.com

Licence

(The MIT Licence)

Copyright (c) 2012 Vincent Voyer, http://zeroload.net http://fasterize.com

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.

lazyload's People

Contributors

gmarty avatar pioupioum avatar vvo avatar

Watchers

 avatar  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.