首页 > 编程 > JS > 正文

图片自动按宽高比例缩小的js代码

2020-09-12 13:02:21
字体:
来源:转载
供稿:网友
<script type="text/JavaScript">
function setImgSize(img,width,height){
var MaxWidth=width;//设置图片宽度界限
var MaxHeight=height;//设置图片高度界限
var HeightWidth=img.offsetHeight/img.offsetWidth;//设置高宽比
var WidthHeight=img.offsetWidth/img.offsetHeight;//设置宽高比
if(img.offsetWidth>MaxWidth){
img.width=MaxWidth;
img.height=MaxWidth*HeightWidth;
}
if(img.offsetHeight>MaxHeight){
img.height=MaxHeight;
img.width=MaxHeight*WidthHeight;
}
}
</script>  

<img src="http://www.mycodes.net/mycodes.gif" border=0  onload="setImgSize(this,150,150);"

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表