Jquery image proportional scaling and Chrome image loading problems

Source: Internet
Author: User

You only need to use a jquery images plug-in to implement image proportional scaling in jquery. However, incompatibility with this plug-in chrome will cause image loading failure, therefore, we only need to make a simple judgment to solve this problem.

Images. js File

The Code is as follows: Copy code

Function ResizeImage (obj, color ){
If (obj. length> 1) {alert ('select error'); return ;}
If (color = undefined) {color = '# ffff ';}
Var width = obj. attr ('oldwidth ');
Var height = obj. attr ('oldheight ');
If (width = undefined) {width = obj. attr ('width'); height = obj. attr ('height ');}
Var pos = obj. attr ('pos ');
If (pos = undefined) {pos = 0 ;}
Var src = obj. attr ('src ');
Var img = new Image ();
Img. src = src;
Var oldwidth = img. width;
Var oldheight = img. height;
Var pw = width/oldwidth;
Var ph = height/oldheight;
Var left = 0;
Var top = 0;
If (pw <ph ){
Img. width = width;
Img. height = oldheight * pw;
Top = parseInt (height-img.height)/2 );
} Else {
Img. height = height;
Img. width = oldwidth * ph;
Left = parseInt (width-img.width)/2 );
}
Obj. attr ('width', img. width );
Obj. attr ('height', img. height );
Obj. attr ('oldwidth ', width );
Obj. attr ('oldheight', height );
Obj. attr ('src', img. src );
Obj.css ('position', 'absolute ');
Obj.css ('left', left );
If(pos=%1%%obj.css ('bottom ', 0 );}
If(pos=%0%%%obj.css ('top', top );}
If(pos1_1_-11_1_obj.css ('top', 0 )}
// Package
If (obj. parent ("div [autosize = 'yes']"). length = 0 ){
Var div = document. createElement ('div ');
Dimensions (div).css ('width', width );
Dimensions (div).css ('height', height );
Watermark (div).css ('position', 'relative ');
Vertex (div).css ('border', 'None ');
$ (Div). attr ('autosize', 'yes ');
Watermark (div).css ('background-color', color );
Obj. wrap (div );
}
Delete img;
}
JQuery. fn. extend ({
Autosize: function (color ){
This. each (function (){
ResizeImage ($ (this), color );
})
}
});

Html code

The Code is as follows: Copy code
<Html>
<Head>
<Title> corn string www. bKjia. c0m </title>
<Script type = "text/javascript" src = "jquery. js" charset = "UTF-8"> </script>
<Script type = "text/javascript" src = "image. js" charset = "UTF-8"> </script>
</Head>
<Body>
<Div>

</Div>
<Script type = "text/javascript">
$ (Function (){
If ($. browser. webkit ){
$ ("Img. autosize"). load (function (){
$ (This). autosize ();
})
} Else {
$ ("Img. autosize"). autosize ();
}
});
</Script>
</Body>
</Html>

Note that the Chorme browser is special in the image loading mechanism, which sometimes fails to get the width and height of the image, thus affecting js operations.
Let's say we should first use the $. browser. webkit method of Jquery to determine whether the browser is a Google browser, and then use the. load () method to load the image and then perform the corresponding operations.

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.