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

Javascript
window.onload 加载完毕的问题及解决方案(下)
jQuery 版本的文本输入框检查器Input Check
jquery Firefox3.5中操作select的问题
jquery 1.3.2 IE8中的一点点的小问题解决方法
javascript div 遮罩层封锁整个页面
javascript 页面只自动刷新一次
Javascript 区别浏览器 代码
checkbox 复选框不能为空
Prototype 学习 Prototype对象
Prototype 学习 工具函数学习($方法)
Prototype 学习 工具函数学习($A方法)
Prototype 学习 工具函数学习($w,$F方法)
Prototype Object对象 学习
Prototype Function对象 学习
Prototype Date对象 学习
JavaScript 事件对象的实现
JavaScript 事件查询综合
JavaScript 继承详解(一)
JavaScript 继承详解(二)
JavaScript 继承详解(三)

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-10-12   浏览: 192 ::
收藏到网摘: 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)">