javascript控制图片自适应大小
01
02
03 //图片自适应大小
04 //id为img控件的id
05 //width与height为img父控件的宽高——即img的最大宽高
06 //示例:<img id="dd" src="ee.jpg" onload="imgFit(’’dd’’,100,200)" />"
07 function imgFit(id,width,height)
08 {
09 var imageArr=document.getElementById(id);
10 if(imageArr.offsetWidth>width || imageArr.offsetHeight>height)
11 {
12 imageRate1=parseInt(imageArr.offsetWidth)/width;
13 imageRate2=parseInt(imageArr.offsetHeight)/height;
14 if(imageRate2>imageRate1)
15 imageArr.style.height = imageArr.offsetHeight/imageRate2+"px";
16 else
17 imageArr.style.width = imageArr.offsetWidth/imageRate1 +"px";
18 }
19 }
![](http://www.cnblogs.com/Emoticons/msn/64_64.gif)