This image lazyloader is designed to help you save http requests on images.
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 929 bytes minified gzipped.
How to use
- Add lazyload.min.js to your page before any
<script>tag, either src or inline if you do not have any other scripts in the
- Change all
<img>tags to lazyload :
<img data-src="real/image/src.jpg" src=data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw== onload=lzld(this) onerror=lzld(this) />
Production ready? Yes.
Is it safe to use this piece of software? Don't trust us, trust them:
- mobile.lemonde.fr, news, 1st mobile website in France. source (pdf)
- rue89.com, news, 40 millions page views per month. source
- playtv.fr, tv guide, millions of page views per month. source (pdf)
They all use lazyload for production websites and are happy with it. Customers told us that they cut page download size by 2!
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.
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
How does it works
We built our lazyloader with efficiency and speed in mind.
Many cases are handled, see test/.
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 it is from http://probablyprogramming.com/2009/03/15/the-tiniest-gif-ever
Do not 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).
If you want to automatically add lazyload to your website, contact us at http://fasterize.com
(The MIT Licence)
Copyright (c) 2012 Vincent Voyer, Stéphane Rios
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.