注册 登录
编程论坛 ASP技术论坛

请教:用什么方法可以将图片按比例缩放,但又不是按实际图片大小来显示.使之不变形.

atian 发布于 2008-06-11 22:17, 1543 次点击
请教:如何将图片按比例缩放,使之不变形.
<img border="0" src="<%=pic_url%>" width="120" height="120" align="left" alt="点击查看图片">
5 回复
#2
hugeannex2008-06-11 23:01
用Javascript,我做过,挺不错的。自己想。
#3
hugeannex2008-06-11 23:55
程序代码:
<script language="javascript">  
  function aa(n){
    var img_h=n.clientHeight;
    var img_w=n.offsetWidth;
    
    //缩小50%
    n.height=img_h*0.5;
    n.width=img_w*0.5;
     }
</script>
<body>
<img  src="https://bbs.bccn.net/ad/suhai_468_60.gif" onLoad="aa(this)">
</body>


[[it] 本帖最后由 hugeannex 于 2008-6-12 00:01 编辑 [/it]]
#4
hmhz2008-06-12 08:05
<img border=0 src="https://bbs.bccn.net/images/default/logo.gif" onload="javascript:if(this.width>800)this.width=800">
#5
makebest2008-06-12 09:48
只要不是同时设置 width="120" 和 height="120",总是按比例显示的哦!
#6
天涯听雨2008-06-12 10:07
//图像按比例缩小JS函数
function SmallImg(ImgD){
    var image=new Image();
    image.src=ImgD.src;
    if(image.width>0 && image.height>0){
    flag=true;
    if(image.width/image.height>= 120/90){
     if(image.width>120){
         ImgD.width=120;
         ImgD.height=(image.height*120)/image.width;
     }else{
         ImgD.width=image.width;
         ImgD.height=image.height;
     }
    }
    else{
     if(image.height>90){
         ImgD.height=90;
         ImgD.width=(image.width*90)/image.height;
     }else{
         ImgD.width=image.width;
         ImgD.height=image.height;
     }
    }
  }
}

<img  src="https://bbs.bccn.net/ad/suhai_468_60.gif" onLoad="SmallImg(this)">
1