当前位置: 首页 > 图文教程 > 网络编程 > Javascript > 图片按比例缩放函数

Javascript
AJAX教程(14):通过XMLHTTP加载XML文件
AJAX教程(15):通过XMLHTTP进行一次HEAD请求
AJAX教程(16):通过XMLHTTP进行一次指定的HEAD请求
AJAX教程(17):把XML文件显示为HTML表格
JS通过WMI获取客户端硬件信息
JavaScript中使用远程脚本
关于Ajax技术的注意事项
XMLHttpRequest创建智能表单
iframe创建智能表单
JS教程:线小测试程序
在Web页面中使用计时器
将事件处理器作为HTML标记的属性
事件处理器作为浏览器对象的属性
Math对象应用详解
random()方法和pow()方法
charAt()方法和charCodeAt()方法
Number对象常用的toFixed()方法
JS教程:为什么尽量用局部变量代替全局变量
解决JavaScript循环中的过多操作
处理提示“脚本运行时间过长的提示框”问题

Javascript 中的 图片按比例缩放函数


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-12   浏览: 106 ::
收藏到网摘: n/a

以下是程序代码:
<script language="JavaScript">
<!--
//图片按比例缩放
var flag=false;
function DrawImage(ImgD,iwidth,iheight){
//参数(图片,允许的宽度,允许的高度)
var image=new Image();
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="images/toplogo.gif" onload="javascript:DrawImage(this,100,100)">