Permalink
Switch branches/tags
Nothing to show
Find file
Fetching contributors…
Cannot retrieve contributors at this time
42 lines (26 sloc) 1.69 KB

#unveil.js
###A very lightweight plugin to lazy load images

Most of us are familiar with the Lazy Load plugin by Mika Tuupola.
This plugin is very useful and it boosts performance delaying loading of images in long web pages because images outside of viewport (visible part of web page) won't be loaded until the user scrolls to them.
Lazy Load has some cool options such as custom effects, container, events or data attribute. If you're not gonna use any of them you can reduce the file size by leaving just the essential code to show the images.
That's what I did and this is my lightweight version of Lazy Load - less than 0.5kb.

Visit unveil's project page to read the documentation and see the demo.


Usage

Include the actual image source in a "data-src" attribute.
Use a placeholder image in the src attribute, something to be displayed while the original image loads.

<img src="bg.png" data-src="img.jpg" />
$("img").unveil();

###Option
By default, images are only loaded and "unveiled" when user scrolls to them and they became visible on the screen.
If you want your images to load earlier than that, lets say 200px before they get visible, you just have to:

$("img").unveil( 200 );

###Trigger
You can still trigger image loading whenever you need.
All you have to do is select the images you want to "unveil" and trigger the event:

$("img").trigger( "unveil" );

###Demo
Visit project page to see it working.