JS proportional scaling picture, this function is very practical, when the Web page load a larger picture, will often put a page support deformation, the page becomes difficult to see, so we thought of using JS to control beyond a certain range of pictures, cloud habitat to stabilize the layout of the page, this code segment is completed this function, And the code is very concise, the effect is very good.
Copy Code code as follows:
DrawImage (imgd,iwidth,iheight) {//parameters (picture, allowable width, allowable height)
Var
Image=new Image (); IMAGE.SRC=IMGD.SRC;
if (image.width>0
&& image.height>0) {if (image.width/image.height>=
Iwidth/iheight) {if (image.width>iwidth) {
Imgd.width=iwidth;
imgd.height= (image.height*iwidth)/image.width;
}else{
Imgd.width=image.width;
Imgd.height=image.height;
}
}else{
if (image.height>iheight) {
Imgd.height=iheight;
Imgd.width= (image.width*iheight)/image.height;
}else{
Imgd.width=image.width;
Imgd.height=image.height;
}
}
}
}
</script>Src=http://www.jb51.net/uploadfile/2013/0803/20130803034531502.jpg "
alt= "Effect after auto scale"
Width= "100"
height= "100"
Onload= "Javascript:drawimage (this,80,80)"
/></body>