Jquery image pre-loading auto proportional scaling plug-in

Source: Internet
Author: User

Copy codeThe Code is as follows:
/*
******************
/// Author: no sword (2008-06-23)
// Http://regedit.cnblogs.com

/// Description: A loading flag is displayed before the image is loaded. After the image is downloaded, the image is displayed.
Whether the image can be scaled automatically
This plug-in allows you to load pages first, while loading images later,
It solves the problem of large la s During Scaling only when the image is displayed.
/// Parameter settings:
Auto scaling of scaling or not
Width: Maximum Image Height
Height: Maximum image width
Path of the image loaded by loadpic
*/
JQuery. fn. LoadImage = function (scaling, width, height, loadpic ){
If (loadpic = null) loadpic = "load3.gif ";
Return this. each (function (){
Var t = $ (this );
Var src = $ (this). attr ("src ")
Var img = new Image ();
// Alert ("Loading ")
Img. src = src;
// Auto Scaling
Var autoScaling = function (){
If (scaling ){

If (img. width> 0 & img. height> 0 ){
If (img. width/img. height> = width/height ){
If (img. width> width ){
T. width (width );
T. height (img. height * width)/img. width );
} Else {
T. width (img. width );
T. height (img. height );
}
}
Else {
If (img. height> height ){
T. height (height );
T. width (img. width * height)/img. height );
} Else {
T. width (img. width );
T. height (img. height );
}
}
}
}
}
// The cached image is automatically read under ff.
If (img. complete ){
// Alert ("getToCache! ");
AutoScaling ();
Return;
}
$ (This). attr ("src ","");
Var loading = $ (" ");

T. hide ();
T. after (loading );
$ (Img). load (function (){
AutoScaling ();
Loading. remove ();
T. attr ("src", this. src );
T. show ();
// Alert ("finally! ")
});

});
}

Instructions for use:
$ ("Div img"). LoadImage (true, 120,90 );
The effect is as follows:
Test address http://img.jb51.net/online/jqueryLoadImage/demo.htm

Package and download files

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.