Skip to content

Instantly share code, notes, and snippets.

@millken
Created July 28, 2010 07:08
Show Gist options
  • Save millken/493654 to your computer and use it in GitHub Desktop.
Save millken/493654 to your computer and use it in GitHub Desktop.
/*
**************图片预加载插件******************
///作者:没剑(2008-06-23) http://regedit.cnblogs.com
///参数设置:
scaling 是否等比例自动缩放
width 图片最大高
height 图片最大宽
loadpic 加载中的图片路径
*/
jQuery.fn.LoadImage=function(scaling,width,height,loadpic){
if(loadpic==null)loadpic="load3.gif";
return this.each(function(){
var t=$(this);
var src=$(this).attr("src")
var img=new Image();
//alert("Loading...")
img.src=src;
//自动缩放图片
var autoScaling=function(){
if(scaling){
max = Math.max(img.width/width, img.height/height);
if(max>1)t.css({'width':img.width/max,'height':img.height/max});
}
}
//处理ff下会自动读取缓存图片
if(img.complete){
//alert("getToCache!");
autoScaling();
return;
}
$(this).attr("src","");
var loading=$("<img alt=\"加载中...\" title=\"图片加载中...\" src=\""+loadpic+"\" />");
t.hide();
t.after(loading);
$(img).load(function(){
autoScaling();
loading.remove();
t.attr("src",this.src);
t.show();
//alert("finally!")
});
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment