网页里自动调整图片大小的问题

来源:百度知道 编辑:UC知道 时间:2024/05/16 08:47:28
我现在写了个js自动调整图片大小的程序,这个js是在图片的onload事件里调用的,现在有个问题:当图片在load的时候,他仍然会以原来的大小显示,当load完时,js才会把他调整,这样在load的过程中仍然会破坏页面啊,还有就是当图片显示不出时,就调整不到规定的大小,这样也会破坏页面。
也就是说现在是先显示再调整大小,我想要先调整大小,再显示,这样就不会破坏页面了,请问有什么方法

没必要那么麻烦,一个思路
css里设置img {display:none}
window.onload之后循环跑image的大小的时候写个xx.style.display="block"

很简单,在这个图片后面照样加上宽和高的设置,像这样,我这里把限制图片的代码也发上来了

<script language="JavaScript">
<!--
//图片按比例缩放
var flag=false;
function DrawImage(ImgD,iwidth,iheight){
//参数(图片,允许的宽度,允许的高度)
var image=new Image();
image.src=ImgD.src;
if(image.width>0 && image.height>0){
flag=true;
if(image.width/image.height>= iwidth/iheight){
if(image.width>iwidth){
ImgD.width=iwidth;
ImgD.height=(image.height*iwidth)/image.width;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+"×"+image.height;
}
else{
if(image.height>iheight){
ImgD.height=iheight;
ImgD.width=(image.width*iheight)/image.height;
}else{
ImgD.width=image.width;
ImgD.height=image.height;
}
ImgD.alt=image.width+&q