How to use the jQuery plug-in for Lazy Load 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. this is the opposite of the image pre-loading method.

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.

Jquery-lazy-load-plugin

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>

You must modify the HTML code. set the booth operator image in the src attribute. The demo page uses 1x1 pixel gray GIF image. in addition, you need to set the URL of the real image to the data-original attribute. a specific class can be defined here to obtain the image object to be loaded with delay. in this way, you can easily control plug-in binding.

The image processing code is as follows.

$ ("Img. lazy"). lazyload ();

This will delay loading of all images whose classes are lazy. For details, refer to the basic options demo.

Set sensitivity

JavaScript is activated in almost all browsers. however, you may still want to display real images on clients that do not support JavaScript. when the browser does not support JavaScript, you can use the <noscript> label to write the actual image fragments.


<Noscript> </noscript>

You can use CSS to hide placeholders.

. Lazy {
Display: none;
}

In browsers that support JavaScript, you must display the placeholder during DOM ready, which can be completed at the same time as the plug-in initialization.

$ ("Img. lazy"). show (). lazyload ();

These are optional, but you should do this if you want the plug-in to downgrade smoothly.

 
By default, images are loaded when they are displayed on the screen. If you want to load images in advance, you can set the threshold option to set threshold to 200 so that the images are loaded in advance when they are 200 pixels away from the screen.

$ ("Img. lazy"). lazyload ({threshold: 200 });

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.

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 position of the image in 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. this is the demo page.

$ ("Img. lazy"). lazyload ({
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.

# Container {
Height: 600px;
Overflow: scroll;
}

$ ("Img. lazy"). lazyload ({
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. lazy"). lazyload ({
Failure_limit: 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 ({
Event: "sporty"
});
});
$ (Window). bind ("load", function (){
Var timeout = setTimeout (function () {$ ("img. lazy"). trigger ("sporty")}, 5000 );
});

Load hidden images

You may have hidden images on your page. for example, if the plug-in is used to filter the list, you can constantly modify the display status of each item in the list. to improve performance, Lazy Load ignores hidden images by default. if you want to load a hidden image, set skip_invisible to false.

$ ("Img. lazy"). lazyload ({
Skip_invisible: false
});

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.