window.onload=function(){
ImgAuto(200,200);
}

解决方案 »

  1.   

    <script type="text/javascript">
    function loadlargeimage(myimg,mywidth,myheight){
    var tmp_img = new Image();
    tmp_img.src=myimg.src;
    image_x=tmp_img.width;
    image_y=tmp_img.height;
    if(image_x > mywidth){
    tmp_img.height=image_y * mywidth / image_x;
    tmp_img.width=mywidth;
    if(tmp_img.height>myheight){
    tmp_img.width=tmp_img.width * myheight / tmp_img.height;
    tmp_img.height=myheight;
    }
    }else if(image_y > myheight)
    {
    tmp_img.width=image_x * myheight / image_y;
    tmp_img.height=myheight;
    if(tmp_img.width>mywidth){
    tmp_img.height=tmp_img.height * mywidth / tmp_img.width;
    tmp_img.width=mywidth;
    }
    }
    myimg.width=tmp_img.width;
    myimg.height=tmp_img.height;}
    </script>
    <img src="aaa.gif" onload="loadlargeimage(this,200,200)">
      

  2.   

    用window.onload=ImgAuto(200,200) 肯定会出错的,因为图片都没加载到页面,
    方法是:页面加载的图片先隐藏,在加载完后用ImgAuto(宽,高);例如:
    <html>
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    <title>缩略图</title>
    <script>
    function ImgAuto(MaxWidth,MaxHeight) 

    for (i = 0; i < document.images.length; i++) {
    var img=document.images[i];
    var HeightWidth=img.offsetHeight/img.offsetWidth;
    var WidthHeight=img.offsetWidth/img.offsetHeight;
    if(img.readyState!="complete"){
    return false;
    }
    if(img.offsetWidth>MaxWidth){
     img.width=MaxWidth;
     img.height=MaxWidth*HeightWidth;
    }
    if(img.offsetHeight>MaxHeight){
     img.height=MaxHeight;
     img.width=MaxHeight*WidthHeight;
    }
    }
    }
    function show_all(divId){
     var img_f=document.getElmentById(divId);
     img_f.style.display="block";
    }
    </script>
    </head>
    <body>
    <div id="img_frame" style="display:none">
    <img src="/../**.gif"/>
    <img src="/../**.gif"/>
    <img src="/../**.gif"/>
    <img src="/../**.gif"/>
    </div>
    <script type="text/javascript>
      ImgAuto(200,200);
      show_all("img_frame"); 
    </script>
    </body>
    </htm>