<div class="codetitle"> <span><a style="CURSOR: pointer" data="12157" class="copybut" id="copybut12157" onclick="doCopy('code12157')"><u>复制代码</u></a></span> 代码如下:</div> <div class="codebody" id="code12157"> <br><script type="text/javascript" src="js/jquery.min.js"></script> <br><script type="text/javascript"> <BR>jQuery.fn.LoadImage=function(scaling,width,height,loadpic){ <BR>if(loadpic==null)loadpic="../img/loading.gif"; <BR>return this.each(function(){ <BR>var t=$(this); <BR>var src=$(this).attr("src") <BR>var img=new Image(); <BR>img.src=src; <BR>//自动缩放图片 <BR>var autoScaling=function(){ <BR>if(scaling){ <BR>if(img.width>0 && img.height>0){ <BR>if(img.width/img.height>=width/height){ <BR>if(img.width>width){ <BR>t.width(width); <BR>t.height((img.height*width)/img.width); <BR>}else{ <BR>t.width(img.width); <BR>t.height(img.height); <BR>} <BR>} <BR>else{ <BR>if(img.height>height){ <BR>t.height(height); <BR>t.width((img.width*height)/img.height); <BR>}else{ <BR>t.width(img.width); <BR>t.height(img.height); <BR>} <BR>} <BR>} <BR>} <BR>} <BR>//处理ff下会自动读取缓存图片 <BR>if(img.complete){ <BR>autoScaling(); <BR>return; <BR>} <BR>$(this).attr("src",""); <BR>var loading=$("<img alt=\"加载中...\" title=\"图片加载中...\" src=\""+loadpic+"\" />"); <BR>t.hide(); <BR>t.after(loading); <BR>$(img).load(function(){ <BR>autoScaling(); <BR>loading.remove(); <BR>t.attr("src",this.src); <BR>t.show(); <BR>}); <BR>} ); <BR>} <BR></script> <br><div id="content"><img src="img/20120518073933709.jpg" alt="JS自动缩小超出大小的图片_jquery" ></div> <br><script type="text/javascript"> <BR><!-- <BR>$(window).load(function(){ <BR>$('#content img').LoadImage(true, 600,500,'img/loading.gif'); <BR>}); <BR>//--> <BR></script> <br> </div>