How to Use jquery. lazyload to dynamically delay page 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. this is the opposite of the image pre-loading method.

Delayed loading of images on pages that contain 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.

How to use it?

Lazy Load depends on jQuery. Add the following code to the head area of the page ::

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

$("#phpernote").lazyload();

This will delay loading the image id = "phpernote.

Set Sensitivity

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

$("#phpernote").lazyload({ threshold : 200 });

Set the critical value to 200. When the visible area is 200 pixels away from the image, the image will be loaded. (the literal meaning of this sentence is different from my understanding. Original: 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.

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

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

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

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

$("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. this is the demo page for delayed loading ..

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

Jquery. lazyload Dynamic Delay loading page image source code download

Articles you may be interested in
  • Detailed usage of the jQuery plug-in (Lazy Load) for delayed image loading
  • JavaScript and Jquery dynamically load Js files and Css files
  • Js address bar special effects (display the size of all images with links on the page and view the height of the current browser)
  • How to Use jquery to control the display of user-uploaded pictures with too much width
  • JQuery adds Event Response to dynamically generated content (jquery live Method Introduction)
  • Jquery provides the ability to load data as the scroll bar scrolls.
  • DIV layer that slides with the scroll bar (fixed to the DIV layer that does not disappear with browsing on the top of the page)
  • Insert element in js to the front of the page body

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.