日期:2014-05-16  浏览次数:20451 次

太神奇了,大家来看看怎么回事
这个是调整图片大小的函数,让人想不通的执行完ImgD.width=160;ImgD.height竟然等于0,到底是哪出问题了啊

function   adjustsize(ImgD)
{
        var   image=new   Image();
image.src=ImgD.src;
if(image.width> 0   &&   image.height> 0)
{
            if(image.width> image.height)
    {

          ImgD.width=160;
          ImgD.height=image.height*160/image.width;
          ImgD.vspace=ImgD.vspace+(160-ImgD.height)/2;
          if(ImgD.height==0)
                alert( "高: "+image.height+ "     宽: "+image.width+ "     字: "+ImgD.vspace   );
    }
  else
  {
          ImgD.height=160;
  ImgD.width=image.width*160/image.height;  
  }
  ImgD.style.visibility= "visible ";
}
}

------解决方案--------------------
你得确保ImgD是否加载完成。ImgD都还没下载完哪来的宽高?
------解决方案--------------------
楼上正解。。。

为什么要new一个Image对象?在图片onload的时候直接判断就可以了
------解决方案--------------------
<img src= "abc.jpg " onload= "adjustsize(this) " style= "display:none ">
------解决方案--------------------
<html>
<head>
<meta http-equiv= "Content-Type " content= "text/html; charset=gb2312 ">
<title> JK:支持民族工业,尽量少买X货 </title>
<script>
function setMaxHeight(imgObj,maxHeight){
if(imgObj.offsetHeight> maxHeight) {
imgObj.height=maxHeight;
imgObj.removeAttribute( 'width ');
}
}
</script>
</head>
<body>
<div style= "font-size:10pt; ">
注1:设定图片的最大宽度或高度,同时保证图片比例。 <br/>

<br/>
注:本页面仅在IE6/FireFox1.5下测试过。其它浏览器或其它版本未经测试。 <br/>
注-----:JK: <a href= "mailTo:jk_10000@yahoo.com.cn?subject=About IMG "> JK_10000@yahoo.com.cn </a> <br/>
<hr/>
</div>

<IMG id= "big_img2 "
src= "http://img.phoenixtv.com/tres/recommend/phoenixtv/scnews/070828-news-950x90.gif " width= "200 " onload= "setMaxHeight(this,300); ">
</body>
</html>