您的位置:首页 > Web前端 > JavaScript

js等比例缩放图片

2011-11-29 10:45 344 查看
<script language="JavaScript">

//图片按比例缩放

var flag=false;

function DrawImage(ImgD){

var image=new Image();

var iwidth = 80; //定义允许图片最大宽度

var iheight = 80; //定义允许图片最大高度

image.src=ImgD.src;

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

flag=true;

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;

}

ImgD.alt=image.width+"×"+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;

}

ImgD.alt=image.width+"×"+image.height;

}

}

}

</script>

<img src=".." onload = "DrawImage(this)">
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: