图片按比例缩放JS

作者:我就是个世界 发表于:2008-01-13

调用方法:
1、当然还是先在要用的页面调用这个JS
[code]<script src="/js/img.js"></script>[/code]

2、在要缩略的图片上加上 onload=DrawImage(this,长,宽)
例如:[code]<img src="#" onload="DrawImage(this,60,60)"[/code]


将以下代码保存为[color=#FF0000][b]img.js[/b][/color]
[separator]
[code]//使用onload=DrawImage(this,长,宽);
function DrawImage(ImgD,w,h){
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= w/h){
if(image.width>w){
ImgD.width=w;
ImgD.height=(image.height*w)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
//ImgD.alt=image.width+"×"+image.height;
}
else{
if(image.height>h){
ImgD.height=h;
ImgD.width=(image.width*h)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
//ImgD.alt=image.width+"×"+image.height;
}
}
}[/code]

分享:

扫一扫在手机阅读、分享本文

请发表您的评论