jquery picture equal scaling and chrome loading picture problem

Source: Internet
Author: User

Images.js file

The code is as follows Copy Code

function Resizeimage (obj,color) {
if (obj.length>1) {alert (' select Error ');
if (color==undefined) {color= ' #ffffff ';}
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 (pos==-1) {obj.css (' top ', 0)}
Package
if (obj.parent ("div[autosize= ' yes ')"). Length==0) {
var div=document.createelement (' div ');
$ (div). css (' width ', width);
$ (div). css (' height ', height);
$ (div). css (' position ', ' relative ');
$ (div). css (' border ', ' none ');
$ (div). attr (' AutoSize ', ' yes ');
$ (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
<title> Corn Skewer www.111cn.net</title>
<script type= "Text/javascript" src= "Jquery.js" charset= "Utf-8" ></script>
<script type= "Text/javascript" src= "Image.js" charset= "Utf-8" ></script>
<body>
<div>

</div>
<script type= "Text/javascript" >
$ (function () {
if ($.browser.webkit) {
$ ("Img.autosize"). Load (function () {
$ (this). AutoSize ();
})
}else{
$ ("Img.autosize"). AutoSize ();
}
});
</script>
</body>

Here is a problem to note that in the loading picture mechanism Chorme Browser is more special, resulting in sometimes not get the picture width and height, thus affecting the operation of JS.
We have to use the $.browser.webkit method of jquery to determine whether the browser is Google Browser, and then use the. Load () method to make the picture load complete and then perform the appropriate action.

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.