jQuery图片预加载 等比缩放实现代码
更新时间:2011年10月04日 19:42:18 作者:
jQuery图片预加载 等比缩放实现代码,需要的朋友可以参考下。
复制代码 代码如下:
/*
* Image preload and auto zoom
* scaling 是否等比例自动缩放
* width 图片最大高
* height 图片最大宽
* loadpic 加载中的图片路径
* example $("*").LoadImage(true,w,h);
*/
jQuery.fn.LoadImage=function(scaling,width,height,loadpic){
if(loadpic==null)loadpic="../images/loading.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){
if(img.width>0 && img.height>0){
if(img.width/img.height>=width/height){
if(img.width>width){
t.width(width);
t.height((img.height*width)/img.width);
t.css("margin-top", (height-t.height())/2);
}else{
t.width(img.width);
t.height(img.height);
t.css("margin-top", (height-t.height())/2);
}
}
else{
if(img.height>height){
t.height(height);
t.width((img.width*height)/img.height);
t.css("margin-top", (height-t.height())/2);
}else{
t.width(img.width);
t.height(img.height);
t.css("margin-top", (height-t.height())/2);
}
}
}
}
}
//处理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!")
});
});
}
相关文章
jQuery 瀑布流 绝对定位布局(二)(延迟AJAX加载图片)
瀑布流-绝对定位布局,与浮动布局的区别在于布局不一样,AJAX不一样2012-05-05
jquery ui resize 中border-box的bug修正
本文给大家分享的是jQuery ui resize中的一个样式的小bug的解决方法,官方并没有修复,这里推荐给大家,有需要的小伙伴可以参考下。2015-04-04
jQuery formValidator表单验证插件开源了 含API帮助、源码、示例
ajaxValidator函数在提交后发生错误,停留在本页面后,再次触发校验会让提示内容一直处于onload状态。2008-08-08


最新评论