How to Implement image pre-loading and delayed loading using Jquery

Source: Internet
Author: User
This article mainly introduces Jquery's methods for implementing image pre-loading and delayed loading, and introduces native javascript and jQuery plug-ins for implementing image pre-loading and delayed loading, it has some reference value. If you need it, you can refer to the following example to describe how Jquery implements image pre-loading and delayed loading. Share it with you for your reference. The specific analysis is as follows:

There are many projects that often need to determine whether to perform the corresponding operations after the image is loaded, or the image loading needs to be delayed. Although there are already good plug-ins on the internet, however, if you have to load a plug-in separately for these effects, you may feel a little uncomfortable. You just wrote a method yourself:

The Code is as follows:

Function loadimg (arr, funLoading, funOnLoad, funOnError ){
Var numLoaded = 0,
NumError = 0,
IsObject = Object. prototype. toString. call (arr) = "[object Object]"? True: false;

Var arr = isObject? Arr. get (): arr;
For (a in arr ){
Var src = isObject? $ (Arr [a]). attr ("data-src"): arr [a];
Preload (src, arr [a]);
}

Function preload (src, obj ){
Var img = new Image ();
Img. onload = function (){
NumLoaded ++;
FunLoading & funLoading (numLoaded, arr. length, src, obj );
FunOnLoad & numLoaded = arr. length & funOnLoad (numError );
};
Img. onerror = function (){
NumLoaded ++;
NumError ++;
FunOnError & funOnError (numLoaded, arr. length, src, obj );
}
Img. src = src;
}
}


Parameter description:

Arr: it can be an array storing the image path, or the jquery object of the selected img;
FunLoading: The operation performed after each individual image is loaded;
FunOnLoad: operation after all images are loaded;
FunOnError: operation when an error occurs during image loading.

For example:

The Code is as follows:

Var imgonload = function (errors ){
/* Errors: number of images with errors loaded ;*/
Console. log ("loaded," + errors + "images loaded error! ");
}

Var funloading = function (n, total, src, obj ){
/*
N: Number of loaded instances;
Total: total number of images to be loaded;
Src: Path of the currently loaded image;
Obj: when the input arr in the loadimg function is an array storing the image path, obj = src is the image path,
When arr is a jquery object, obj is the currently loaded img dom object.
*/
Console. log (n + "of" + total + "pic loaded.", src );
Var newimg = document. createElement ("img ");
Newimg. src = src;
$ ("Body"). append (newimg). fadeIn ();
}

Var funloading_obj = function (n, total, src, obj ){
Console. log (n + "of" + total + "pic loaded.", src );
$ (Obj). attr ("src", src );
$ (Obj). fadeIn (200 );
}

Var funOnError = function (n, total, src, obj ){
Console. log ("the" + n + "st img loaded Error! ");
}


Call example:

The Code is as follows:

Console. log ("loading ...");
Loadimg ($ ("img"), funloading_obj, imgonload, funOnError );
/* Loadimg (["http://pic22.nipic.com/20120619/9607634_212642465144_2.jpg ",
"/20120531/1670912 _103610084349_2.jpg ",
"/20120616/4952071 _130629530136_2.jpg ",
"/20120610/1723580 _105037029000_2.jpg ",
Http://pic22.nipic.com/20120617/2572038_125013326121_2.jpg"
], Funloading, imgonload, funOnError );*/


The above is the original style. Next we will introduce a jQuery plug-in based on Lazy Load that delays image loading.

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

The Code is as follows:

Related Article

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.