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

Javascript
对YUI扩展的Gird组件 Part-1
xtree.js 代码
newxtree.js代码
jQuery使用手册之一
jQuery使用手册之二 DOM操作
Javascript中eval函数的详细用法与说明
JavaScript 中的事件教程
JavaScript初级教程(第二课)
JavaScript初级教程(第三课)
JavaScript初级教程(第四课)
JavaScript初级教程(第五课)
诘屈聱牙之javascript中国象棋
filemanage功能中用到的common.js
filemanage功能中用到的lib.js
Javascript中eval函数的使用方法与示例
用javascript控制iframe滚动的代码
总结两个Javascript的哈稀对象的一些编程技巧
疯掉了,尽然有js写的操作系统
用javascript实现select的美化的方法
javascript+dom树型菜单类,希望朋友们一起进步

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


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