Copy codeThe Code is as follows:
/*
* Image preload and auto zoom
* Whether scaling scales proportionally automatically
* Width: Maximum Image Height
* Height: Maximum image width
* Path of the image loaded by loadpic
* Example $ ("*"). LoadImage (true, w, h );
*/
JQuery. fn. LoadImage = function (scaling, width, height, loadpic ){
If (loadpic = null) loadpic = "../images/loading.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 );
T.css ("margin-top", (height-t.height ()/2 );
} Else {
T. width (img. width );
T. height (img. height );
T.css ("margin-top", (height-t.height ()/2 );
}
}
Else {
If (img. height> height ){
T. height (height );
T. width (img. width * height)/img. height );
T.css ("margin-top", (height-t.height ()/2 );
} Else {
T. width (img. width );
T. height (img. height );
T.css ("margin-top", (height-t.height ()/2 );
}
}
}
}
}
// 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! ")
});
});
}