您的位置:首页 > Web前端 > JavaScript

js按比例缩放图片

2010-12-02 09:15 246 查看
//使用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;

}

}

}

调用方法:

1、当然还是先在要用的页面调用这个JS

<script src="/js/img.js"></script>

2、在要缩略的图片上加上 onload=DrawImage(this,长,宽)

例如:<img src="#" onload="DrawImage(this,60,60)"
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: