lazierLoad – Javascript Image Lazy Loader for Prototype

Overview



What is lazierLoad?

lazierLoad is Bramus! his take at writing a delayed javascript image loader for use with PrototypeJS.

lazierLoad automatically hooks itself to the page, finds all images and only loads those appearing “above the fold” resulting in faster page loads. The images not located in the viewport, are not loaded until they appear within it (viz. when the user scrolls down).

The idea for lazierLoad was inspired upon Lazy Load (which is built for use with jQuery), yet the code differs quite a lot.

top



Demo

lazierload.gif
(Pictures by Pieter Morlion)

top



Download

Please download the latest version below:

Latest Version

Archived Versions

Don't forget to read the License & usage and Installation/Configuration blocks on this page! ;)

top



Version History

Version 0.4 - 2008.02.28

  • ADD: ability to automatically autoLoad or not
  • UPD: backdrop from 0.2 where one could set options through a new instantiation, enabling one to have per page options

Version 0.3 - 2008.02.26

  • ADD: options: minWidth, minHeight, imgTypes
  • UPD: moved all options to global Object
  • UPD: works with latest Prototype (1.6.0.2)

Version 0.2 - 2007.09.12

  • ADD: options: treshold, loadingImage, replaceImage

Version 0.1 - 2007.09.11 - * INITIAL RELEASE *

  • Well ... first release.

top


License & usage

The script is now released under a creative commons Attribution-ShareAlike 2.5 license. Should you use the script within a commercial context please think about clicking the PayPal donate button.

top


Installation/Configuration

Installation

lazierLoad depends on PrototypeJS 1.6.0 Final or greater (1.6.0 beta or rc won't work!). Prototype 1.6.0.2 is included in the release ;)

  1. Include the needed javascript (2 files) in your HTML document

    HTML:
    1. <!-- lazierLoad prerequisites : prototype.js - version 1.6.0 final or greater required! -->
    2. <script type="text/javascript" src="js/lib/prototype.js"></script>
    3.    
    4. <!-- lazierLoad core -->
    5. <script type="text/javascript" src="js/bramus/lazierLoad.js"></script>
  2. That's it, you're done!

Configuration

Automatic hooking of lazierLoad - Configuring the default options

If you are not happy with the default settings, you can tweak lazierLoad by editing the lazierLoadDefaultOptions Object in the lazierLoad.js file.

Possible options to tweak are:

  • treshold: images which appear treshold pixels from the bottom will be loaded
    (defaults to “100”)
  • replaceImage: image to replace the non-loaded images with until they are loaded (most likely a transparent 1 by 1 pixel image)
    (defaults to “blank.gif”)
  • loadingImage: image to show while a non-loaded image is being loaded
    (defaults to “spinner.gif”)
  • extensions: array of extensions to lazyLoad
    (defaults to “['gif','jpg','png','jpg']”)
  • minWidth: minimum width an image must have in order to be lazyLoaded
    (defaults to “100”)
  • minHeight: minimum height an image must have in order to be lazyLoaded
    (defaults to “100”)

Manual hooking of lazierLoad - Configuring page-specific options

If you want different options on different pages, then set lazierLoadAutoHook to false and manually create a new JS_BRAMUS.lazierLoad instance yourself. Within that manual instantiation you can pass in specific parameters. If set, these parameters will override the default parameters set in the lazierLoadDefaultOptions Object.

Some examples are (only include one instantiation per page though!):

JavaScript:
  1. Event.observe(document, 'dom:loaded', function() {
  2.     myCustomLL = new JS_BRAMUS.lazierLoad({treshold: 200})// Override treshold
  3. }, false);

JavaScript:
  1. Event.observe(document, 'dom:loaded', function() {
  2.     myCustomLL = new JS_BRAMUS.lazierLoad({loadingImage: 'bigspinner.gif', minWidth : 200, minHeight; 200}); // Override spinner, minWidth and minHeight
  3. }, false);

JavaScript:
  1. Event.observe(document, 'dom:loaded', function() {
  2.     myCustomLL = new JS_BRAMUS.lazierLoad({extensions: ['png','jpg']}); // Override extensions : Only JPG's and PNG's
  3. }, false);

JavaScript:
  1. Event.observe(document, 'dom:loaded', function() {
  2.     myCustomLL = new JS_BRAMUS.lazierLoad({treshold : 150, extensions : ['png','jpg','jpeg'], replaceImage : "blank.gif", loadingImage : "bigspinner.gif", minWidth : 200,  minHeight : 200}); // Override all options.
  3. }, false);


top


Current Limitations

  • Lazy loading does not currently work with safari because of webkit bug #6656 (same goes for Lazy Load).

top



Feedback/Help/Support

One can always leave a comment in case any questions should arise or if a bug were to be found; and one can always support me by donating me a tidbit to my paypal ;)

top


Make a donation

It certainly is no obligation but it would most certainly put a smile on my face ;) Alternatively you can support me by clicking one of the ads below.

Advertisements

Spread the word!

  • del.icio.us
  • digg
  • Ma.gnolia

About this Page

Best of Bram.us