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

Javascript
jQuery1.3全新的Sizzle引擎实现CSS选择器
网页里做异步的跨域请求
WEBJX收集9个小巧实用的jQuery插件
JavaScript教程:switch-case
Javascript代码:校验身份证号程序
Webjx收集jQuery图片切换效果插件
JS触发A标签的点击事件
把Javascript代码放到body结束之上
jQuery教程:认识jQuery
Javascript 验证表单插件
javascript教程:call方法
不同页面中调用JS代码乱码问题
JavaScript获取事件对象的注意事项
非常简单的jQuery实现网页图片圆角
史上最昂贵的 Javascript 代码
学习JavaScript后的小结
Javascript教程:caller函数和callee属性
解决用户恶意刷新的二级高亮样式菜单
PS教程:用Photoshop模拟日全食
JS实例教程:检查变量类型

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


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