JQuery Plugin: Image lazy loader Image loading Delay

Source: Internet
Author: User

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 pageheadRegion:

12
<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 delayed loading of images in the id = "http://www.appelsiini.net/projects/lazyload/img" area.

Set Sensitivity

The plugin providesthresholdYou 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. The original text is: 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 pixels images are included in the plug-in.

1
$("img").lazyload({ placeholder : "img/grey.gif" });
Event trigger Loading

Events can be any jQuery time, such:clickAndmouseoverYou can also use custom events, such:sportyAndfoobarBy 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:

1234
$("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 usesFadeInResult: This is the demo page.

1234
$("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:

1234
#container {height: 600px;overflow: scroll;}

JavaScript code:

1234
$("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. check whether the image is in the visible area in a loop. by default, the loop is stopped when the first image is not visible. images are regarded as stream distribution. The order of images on the page is the same as that in HTML code. however, in some la s, such assumptions are not true. however, you can usefailurelimitOption to control the loading behavior.

123
$("img").lazyload({failurelimit : 10});

SetfailurelimitIf 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.

123456789
$(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 very rare problem. If someone finds a solution, contact the author.

 

From: http://www.appelsiini.net/projects/lazyload

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.