Reprinted: http://www.neoease.com/lazy-load-jquery-plugin-delay-load-image/
Lazy loadIs a jquery plug-in written in Javascript. It canDelay loading of images on long pages. Images outside the visible area of the browser will not be loaded until the user scrolls the page to their location. This is the opposite of the processing method of image pre-loading.
Delayed loading of images on pages containing many large images can speed up page loading. The browser will be ready after the visible images are loaded. In some cases, it can help reduce the server load.
Lazy load is inspired by the Yui imageloader toolbox created by Matt mlinac. This is the demo page.
How to use it?
Lazy load depends on jquery. Please add the following code to the pagehead
Region:
1 2
|
<script src="jquery.js" type="text/javascript"></script> <script src="jquery.lazyload.js" type="text/javascript"></script> |
Add the following statement to your Execution Code:
1
|
$("http://www.appelsiini.net/projects/lazyload/img").lazyload(); |
This causes the image in the id = "http://www.appelsiini.net/projects/lazyload/img" area to be delayed.
Set Sensitivity
The plugin providesthreshold
You can control the image loading by setting the critical value (the distance from the trigger to the image). The default value is 0 (when the image boundary is reached ).
1
|
$("http://www.appelsiini.net/projects/lazyload/img").lazyload({ threshold : 200 }); |
Set the critical value to 200. When the visible area is 200 pixels away from the image, the image is loaded. (the literal meaning of this sentence is different from my understanding:
Setting threshold to 200 causes image to load 200 pixels before it is
Visible .)
Placeholder Image
You can also set a placeholder image and define an event to trigger the loading action. in this case, you need to set a URL for the placeholder image. transparent, gray, and white 1x1 pixel images are included in the plug-in.
1
|
$("img").lazyload({ placeholder : "img/grey.gif" }); |
Event trigger Loading
Events can be any jquery time, such:click
Andmouseover
You can also use custom events, such:sporty
Andfoobar
By default, the system is waiting until the user rolls to the location of the window. You can stop loading the picture before the gray placeholder image is clicked. You can do this:
1 2 3 4
|
$("img").lazyload({ placeholder : "img/grey.gif", event : "click" }); |
Use special effects
When the image is fully loaded, the plug-in is used by default.show()
Method to display the graph. In fact, you can use any special effects you want to use. The following code usesFadeIn
Result: This is the demo page.
1 2 3 4
|
$("img").lazyload({ placeholder : "img/grey.gif", effect : "fadeIn" }); |
Picture in Container
You can use the plug-in on the image of the scroll container, such as the DIV element with a scroll bar. all you need to do is define the container as a jquery object and upload it to the initialization method as a parameter. this is a horizontal and vertical scrolling demo page.
CSS code:
1 2 3 4
|
#container { height: 600px; overflow: scroll; } |
JavaScript code:
1 2 3 4
|
$("img").lazyload({ placeholder : "img/grey.gif", container: $("#container") }); |
When images are not arranged in sequence
When you scroll through a page, lazy load cyclically loads images and checks whether the images are in the visible area.
By default, the loop is stopped when you find the first image that is not in the visible area. The image is regarded as a stream distribution, and the order of the image in the page is the same as that in the HTML code.
However, in some la s, such assumptions are not true, but you canfailurelimit
Option to control the loading behavior.
1 2 3
|
$("img").lazyload({ failurelimit : 10 }); |
Setfailurelimit
If this parameter is set to 10, the search will be stopped only when the plug-in finds 10 images that are not in the visible area. If you have a complicated layout, set this parameter a little higher.
Delayed image loading
Lazy load is an incomplete function of the plug-in, but it can also be used for delayed image loading. the following code implements page loading and then loading. after the page is loaded for 5 seconds, images in the specified area are automatically loaded. this is the demo page for delayed loading.
1 2 3 4 5 6 7 8 9
|
$(function() { $("img:below-the-fold").lazyload({ placeholder : "img/grey.gif", event : "sporty" }); }); $(window).bind("load", function() { var timeout = setTimeout(function() {$("img").trigger("sporty")}, 5000); }); |
Download plug-ins
Latest Version: source code, compressed code, and packaged code
Known issues
Due to WebKit bug #6656, lazy load cannot be used in Safari and chrome. It will immediately load all the images you are willing to and are not willing to load for you.
It seems that jquery 1.3.x makes the plug-in invalid in IE. all images will be loaded in the background even if they should not be loaded. the author is working hard to solve this problem, during which the plug-in can only be used in jquery 1.2.6.
Also, if you use mint, add the mint tag to the page header. if you add the mint tag to the end of the page, it will interfere with the lazy load plug-in. this is a fairly rare problem. If someone finds a solution, contact the author.