$3





---

_This project is friendly supported by
JetBrains &
PhpStorm!_
---
$3
*
About Lazy
*
Compatibility
*
Documentation / Examples
*
Installation
*
CDN
*
Self-Hosted
*
Package Managers
*
Basic Usage
*
Callbacks / Events
*
Instances and public Functions
*
Custom Content Loaders
*
Loader Plugins
*
Configuration Parameters
*
Build and Validation
*
Available gulp Tasks
*
Bugs / Feature request
*
License
*
Donation
---
About Lazy
Lazy is a fast, feature-rich and lightweight delayed content loading plugin for jQuery and Zepto.
It's designed to speed up page loading times and decrease traffic to your users by only loading the content in view.
You can use Lazy in all vertical and horizontal scroll ways.
It supports images in
![]()
tags and backgrounds, supplied with css like
background-image, by default.
On those elements Lazy can set an default image or a placeholder while loading and supports retina displays as well.
But Lazy is even able to load any other content you want by
plugins and
custom loaders.
Compatibility
Lazy will work with a wide range of browsers and support jQuery versions for years backwards and Zepto as alternative.
You can pick any version since jQuery 1.7.2 or Zepto 1.1.6 or greater.
There is no way to guarantee, that Lazy will work with all browsers, but all I've tested worked great so far.
If you find any problems in specific browsers,
please let me know.
Tested in: IE, Chrome (+ mobile), Firefox (+ mobile), Safari (+ mobile) and Android Browser.
Documentation / Examples
For
documentation,
examples and other information take a look on the
project page.
Installation
First of all, you will need a copy of
jQuery or
Zepto to use Lazy successfully on your project.
If you get this you can install Lazy by different ways.
Some examples below:
#### CDN
Lazy and all plugins are available over
cdnjs and
jsDelivr CDN and can directly included to every page.
``
HTML
`
#### Self-Hosted
Download and save one of two available files to include Lazy to your page, either the development or the minified version.
`
HTML
`
#### Package Managers
Lazy is even available through NPM and Bower.
Just use one of the following commands below:

`
sh
$ npm install jquery-lazy
$ bower install jquery-lazy
`
Basic Usage
1.) The basic usage of Lazy is pretty easy.
First of all you need to prepare all elements you want to lazy load.
By default add a data-src
attribute to images containing the loadable image and/or a data-loader
attribute to elements witch shall use a plugin or custom loaders.
`
HTML
`
2.) Start using Lazy by calling it after page load.
You don't have to specify your elements exactly, but for better performance, or different options, load your elements over unique classes or any other selector.
`
JS
$(function($) {
$("img.lazy").Lazy();
});
`
Take a look at the documentation to get an idea what Lazy is capable of.
Callbacks / Events
Lazy comes with a bunch of callbacks and events you can assign to.
Just add them by initialization settings:
* beforeLoad
- before item is about to be loaded
* afterLoad
- after the item was loaded successfully
* onError
- whenever an item could not be loaded
* onFinishedAll
- after all items in instance was loaded or returned an error
Instances and public Functions
Lazy supports multiple parallel instances.
Just initialize them with different selectors.
To access an instances public functions you can initialize them in an object oriented manner or grab the instance bind to every element by default:
`
JS
// object oriented way
var instance = $("img.lazy").Lazy({chainable: false});
// grab from elements (only works well if you use same selectors)
$("img.lazy").Lazy();
var instance = $("img.lazy").data("plugin_lazy");
`
Every instance has some public available functions to control it's behavior.
There are currently six available:
`
JS
instance.config(entryName[, newValue]); // get or set an configuration entry
instance.addItems(items); // add new items to current instance
instance.getItems(); // get all unhandled items left of current instance
instance.update([useThrottle]); // loads all elements in current viewport
instance.force(items); // force loading specific items, ignoring the viewport
instance.loadAll(); // loads all remaining available elements from this instance
instance.destroy(); // unbinds all events and stop execution directly
`
Custom Content Loaders
With the custom loaders option there is a powerful solution to load every contents the Lazy way.
Lazy will handle everything, you just create a loading method witch got triggered whenever the element hits the visibility threshold.
It is still possible to load images and custom loaders in the same Lazy instance.
To use this just define a loader function inside the Lazy initialisation and pass the loader name to the data-loader
attribute of the elements witch should be lazy loaded.
`
HTML
`
`
JS
$(".lazy").Lazy({
// callback
beforeLoad: function(element) {
console.log("start loading " + element.prop("tagName"));
},
// custom loaders
customLoaderName: function(element) {
element.html("element handled by custom loader");
element.load();
},
asyncLoader: function(element, response) {
setTimeout(function() {
element.html("element handled by async loader");
response(true);
}, 1000);
}
});
`
Loader Plugins
The loader plugins can extend the functionality of Lazy, like loading other elements and data.
It is basically the same as the custom content loaders, with the difference, that plugins can extend all further instances globally at once permanently and let them handle specific elements like