JS图片压缩–图片后加载后按比例缩放

王朝学院·作者佚名  2009-06-29
窄屏简体版  字體: |||超大  

原理:图片加载完后把图片的尺寸固定在一个固定的范围之内。。

JS Code:

<script type="text/javascript">

var proMaxHeight=100;

var proMaxWidth=100;

function ImgAuto(ImgD)

{

var image=new Image();

image.src=ImgD.src;

image.onload = function(){

if(image.width>0&&image.height>0)

{

var rate=(proMaxWidth/image.width<proMaxHeight/image.height)?proMaxWidth/image.width:proMaxHeight/image.height;

if(rate<=1)

{

ImgD.width=image.width*rate;

ImgD.height=image.height*rate

}

else

{

ImgD.width=image.width;

ImgD.height=image.height;

}

}

};

image.onload();

};

</script>

HTML Code:

<body>

<img src="http://s3.knowsky.com/l/45001-55000/200952916491584421395.jpg" onload="ImgAuto(this)" id="test"/>

<script type="text/javascript" src="img.js"></script>

</body>

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航