Skip to content
forked from vvo/lazyload

Lazyload images, iframes, widgets with a standalone JavaScript lazyloader ~1kb

License

MIT, MIT licenses found

Licenses found

MIT
LICENCE
MIT
LICENCE.fasterize
Notifications You must be signed in to change notification settings

Lazy79/lazyload

 
 

Repository files navigation

lazyload

Lazyload images, iframes, divs, widgets untill they are visible in the viewport.

Build Status

Selenium Test Status

Usage

Make sure you are in standards mode.

Viewport computing is badly handled by browsers when in quirksmode.

<!DOCTYPE html>
<html>
  <head>
    <title>lazyload</title>
  </head>
  <body>
    <script src="lazyload.min.js"></script>

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

    <iframe
      data-src="yourpage.html"
      src="about:blank"
      onload="lzld(this)"></iframe>
  </body>
</html>

Script should be included after the <body> element and before any lazyloaded image.

See more examples.

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.

Options

Here are the options and defaults. All parameters are optional.

var myLzld = lazyload({
  container: document.body,
  offset: 200,
  src: 'data-src' // or function(elt) { return customSrc }
});
  • container: Which element to be used as the viewport
  • offset: When watched element is offsetpx near the viewport bounds, show it (horizontal, vertical)
  • src: Where to find the real src of your element, either in another attribute (data-src) or using a custom function

hidpi images

When giving a function to the src param, you can implement a custom src selector. So you can handle resolution dependent images.

<!DOCTYPE html>
<html>
  <head>
    <title>lazyload</title>
    <script src="lazyload.min.js"></script>
    <script>
      var lazyHd = lazyload({ src: hidpi });

      function hidpi(img) {
        if(window.devicePixelRatio > 1) {
          return img.getAttribute('data-src-hd');
        }
      }
    </script>
  </head>
  <body>
    <img
      data-src-hd="images/src-hd.jpg"
      src="images/src-desktop.jpg"
      onload="lazyHd(this)">
  </body>
</html>

Here you go! You are now loading a standard desktop image and loading the hd version when needed. You could also load a blank image at start and choose either the desktop or hd version.

If your function does not returns anything special then the initial src= image will not be changed.

Testing

npm install -g zuul
zuul --local 8080 -- test/*.js

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

Tests are written with mocha.

Building

Install closure-compiler.

CLOSURE_PATH=~/path/to/closure-compiler/ grunt

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

Sites using lazyload

Millions of pageviews are served using this project:

Licence

Also see LICENCE.fasterize

(The MIT Licence)

Copyright (c) 2012-2013 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.

About

Lazyload images, iframes, widgets with a standalone JavaScript lazyloader ~1kb

Resources

License

MIT, MIT licenses found

Licenses found

MIT
LICENCE
MIT
LICENCE.fasterize

Stars

Watchers

Forks

Packages

No packages published