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

这个图片等比缩放代码造成浏览器崩溃了,怎么办呢?
图片加载完成前使用Loading.gif代替,加载完成后等比缩放;
下面的代码,造成浏览器不断循环发送请求,最后浏览器不堪负荷,崩溃了。
怎么办呢?
问题出于下面注释行中


function imageScale(ImgD,FitWidth,FitHeight){
var holdImg = "../Images/icons/loading.gif";
var image = new Image(),_width,_hegiht;
image.src = ImgD.src;
ImgD.src = holdImg; /*      问题在这行   和 下面最后一行   */
if(image.width>0 && image.height>0){
         if(image.width/image.height>= FitWidth/FitHeight){
             if(image.width>FitWidth){
                 _width=FitWidth;
                 _height=(image.height*FitWidth)/image.width;
             }else{
                 _width=image.width; 
                _height=image.height;
             }
         } else{
             if(image.height>FitHeight){
                 _height=FitHeight;
                 _width=(image.width*FitHeight)/image.height;
             }else{
                _width=image.width; 
                _height=image.height;
             } 
        }
     }
 ImgD.width = _width;
 ImgD.height = _height;
 ImgD.src = image.src; /*  问题在这行 和前面的注释行 */
}

<img src="http://img.my.csdn.net/uploads/201202/16/266283_1329364024zGY2.jpg" onload="imageScale(this,200,200)" />

------解决方案--------------------
  ImgD.src = holdImg; /*      问题在这行   和 下面最后一行   */ 
onload函数里不能再操作src,否则又会执行onload方法,造成了递归使用,是个死循环!!
------解决方案--------------------
ImgD.onload=null; //删除事件绑定试试
ImgD.src = image.src; /*  问题在这行 和前面的注释行 */
------解决方案--------------------
    <script type="text/javascript">
     &nb