网站上传图片尺寸不等,导致图片变形等问题是所有做网站的人都遇到过的问题,编程之家今天跟大家分享一个超简单的等比例缩放图片JS函数。
JS等比例缩放图片函数
<SCRIPT LANGUAGE="JavaScript">
<!--
function DownImage(ImgD,MaxWidth,MaxHeight){
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
var rate = (MaxWidth/image.width < MaxHeight/image.height)?MaxWidth/image.width:MaxHeight/image.height;
if(rate <= 1){
ImgD.width = image.width*rate;
ImgD.height =image.height*rate;
}
}
}
//-->
</SCRIPT>
调用方法:<img src="1.jpg" border="0" id="img" onload="DownImage(this,130,130);"/>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。