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

Javascript
mvc模式分离javascript开发
30个美观的有创意的和引人注目的灵感导航菜单
JS教程:匿名函数
JS教程:childNodes与parentNode
IE浏览器的DOM模型
使用yui3实现最简单拖动
YUI的简单的dom事件
JS区别IE6、IE7、IE8之间的方法
JavaScript教程:Table行定位效果
建立HTML字符串的最快速方法
10个美观实用的jQuery和MooTool日历插件
JS教程:Javascript实现缓动效果
Javascript小游戏贪吃蛇(详细注释)
JavaScript组件开发:分析和设计组件
JavaScript:判断iframe是否加载完成的完美方法
JS特效详解:图片幻灯片切换
JS给文本区域(textarea)字数限制
用js按比例重定图片尺寸
不用组件实现Ajax效果
PHP+JS实现搜索自动提示

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


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