一个改变图像大小的JS,老出问题,adjustsize这个函数是onLoad时才调用的,可调用的时候它的宽和高竟然都是0,怎么办啊。下面的代码可以弹出“0,0”的对话框。function adjustsize(ImgD)
{
if(ImgD.width >0 && ImgD.height>0)
{
if(ImgD.width>ImgD.height)
{
var temp1=ImgD.width/160;
var temp2=ImgD.height;
ImgD.width=160;
ImgD.height=temp2/temp1;
ImgD.vspace=ImgD.vspace+(160-ImgD.height)/2;
}
else
{
var temp1=ImgD.height/160;
var temp2=ImgD.width;
ImgD.height=160;
ImgD.width=temp2/temp1;
}
ImgD.style.visibility="visible";
}
else
{
alert(ImgD.width+","+ImgD.height);
}
}<td width="200" align="center"><img style="visibility:hidden" src=photo/13.jpg vspace=10 border=0 onload="adjustsize(this)"> <br/>
此问题在JS区的地址是
http://community.csdn.net/Expert/topic/5733/5733109.xml?temp=.2805445
解决的哥们顺便去领一下分。
{
if(ImgD.width >0 && ImgD.height>0)
{
if(ImgD.width>ImgD.height)
{
var temp1=ImgD.width/160;
var temp2=ImgD.height;
ImgD.width=160;
ImgD.height=temp2/temp1;
ImgD.vspace=ImgD.vspace+(160-ImgD.height)/2;
}
else
{
var temp1=ImgD.height/160;
var temp2=ImgD.width;
ImgD.height=160;
ImgD.width=temp2/temp1;
}
ImgD.style.visibility="visible";
}
else
{
alert(ImgD.width+","+ImgD.height);
}
}<td width="200" align="center"><img style="visibility:hidden" src=photo/13.jpg vspace=10 border=0 onload="adjustsize(this)"> <br/>
此问题在JS区的地址是
http://community.csdn.net/Expert/topic/5733/5733109.xml?temp=.2805445
解决的哥们顺便去领一下分。
你可以先创建一个Image(在onload之前),把图片load进来再说。var img = new Image();
img.src="图片地址";
这个你可以参考一下:http://news.lnwest.com/program/article.asp?id=471要复杂一点的可以看看:
http://bbs.51js.com/viewthread.php?tid=14837&highlight=
这样操作,你也不需要用隐藏的IMG标签了
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 );
奇怪的是在执行了ImgD.width=160;后再判断ImgD.width等于0时,它竟然还等于0。你给的参考我看了,每一个好像和这个问题不大一样,第二个看不懂了,牛人的东东,我还处在入门阶段,以后看。
觉得现在主要问题就是onload触发时图像的宽 高属性都是0。不知道怎么办啊。回1楼,我的是IE6,你的是什么啊?
我的浏览器是IE6,版本 6.0.2900.2180.xpsp_2_gdr.070227-2254
其实用Image和你这个方法也差不多。理论上来说onload是图片载入完成之后干的事情,你先不要隐藏<img>标签,看看onload是不是正常。说不定还是你图片地址取错了呢。
直接把adjustsize函数里面的代码写在页面</html>的后面
adjustsize(document.all("image1"));
</body></html>
adjustsize(document.all("image1"));
</script>
</body>
....
<img id="img_id" ... />
....
</html>
<script ....>
adjustsize("img_id")
</script> 但是这个方法我觉得还不如onload,毕竟onload是在载入完图片之后触发的事件,而</html>以后图片有可能还没载入进来。不过可以试试这个方法。
哦,谢谢大家了,但还有一个问题就是我的图片太多了啊,怎么使用循环来调用啊?没明白你的意思
不过一码事算一码事,这个帖先结了吧。嘿嘿。
地址:
http://community.csdn.net/Expert/topic/5736/5736479.xml?temp=.2180445