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.