Detailed usage of the jQuery plug-in (Lazy Load) for delayed image loading

Source: Internet
Author: User

Lazy Load is a jQuery plug-in written in JavaScript. it delays loading images on long pages. images outside the visible area of the browser are not loaded until the user scrolls the page to their location. the advantage is that when a page contains many large images, it can delay loading images to speed up page loading. the browser is ready after the visible image is loaded. in some cases, it can also help reduce the burden on the server.

How to use Lazy Load?

Lazy Load depends on jQuery. Therefore, you need to Load the jquery package first. Add the following code to the head area of the page:

<script src="jquery.lazyload.js" type="text/javascript"></script>

Add the following statement to your Execution Code:

$("#example img").lazyload();

This causes the image in the id = "example" area to be delayed.

Set Sensitivity

The plug-in provides the threshold option. You can set the critical value (the distance from the image to which the image is loaded) to control the image loading. The default value is 0 (when the image boundary is reached ).

$("#example 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 .)

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

$("#example img").lazyload({ placeholder : "img/grey.gif" });

Event trigger Loading

Events can be any jQuery time, such as click and mouseover. you can also use custom events, such as sporty and foobar. by default, the system is waiting until the user rolls to the location of the window. you can disable image loading before the gray placeholder image is clicked:

$("#example img").lazyload({placeholder : "img/grey.gif",event : "click"});

Use special effects

When the image is fully loaded, the ins use the show () method by default to display the image. in fact, you can use any special effects you want to use. the following code uses the FadeIn effect.

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

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

JavaScript code:

$("#example 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 use the failurelimit option to control the loading behavior.

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

Set failurelimit to 10 so that the plug-in can find 10 images that are not in the visible area to stop searching. 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.

$(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 instance code

Articles you may be interested in
  • How to Use jquery. lazyload to dynamically delay page image loading
  • How to Use jquery to control the display of user-uploaded pictures with too much width
  • Jquery checks whether the page has been loaded with the jquery File
  • It would be better to load jquery library files with Google
  • Add vcastr3 Video Player Plug-in to FCKeditor
  • Js checks whether the image is loaded and obtains the image width.
  • How to load javascript to improve website performance
  • Windows cannot start the hardware device because its configuration information (in the Registry) is incomplete or damaged. (Code 19) Solution

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.