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

JAVAScript如何实现图片等比缩放

2009-06-12 14:37 423 查看
<script language="JavaScript">
<!--
//图片按比例缩放
var flag=false;
function DrawImage(ImgD){
var image=new Image();
var iwidth = 160; //定义允许图片宽度,当宽度大于这个值时等比例缩小
var iheight = 120; //定义允许图片高度,当宽度大于这个值时等比例缩小
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;
}
}
}
//调用:<img src="图片" onload="javascript:DrawImage(this)">
//-->
</script>

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/chinmo/archive/2008/02/26/2120434.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: