jQuery图片预加载 等比缩放实现代码 - Web前端
作者:98wpeu发布时间:2026-06-07分类:网页前端技术浏览:3
导读:复制代码代码如下:/**ImagePReloadandautoZoom*scaling是否等比例自动缩放*width图片最大高*height图片最大宽*...
复制代码 代码如下:
/*
* 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";
returnthis.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!")
});
});
}
/*
* 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";
returnthis.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!")
});
});
}
相关推荐
- jqeury-easyui-layout问题解决方法 - Web前端
- 40款非常有用的 jQuery 插件推荐(系列一) - Web前端
- jquery(live)中File input的change方法只起一次作用的解决办法 - Web前端
- 基于jQuery的360图片展示实现代码 - Web前端
- JQuery中判断一个元素下面是否有内容或者有某个标签的判断代码 - Web前端
- jQuery页面滚动浮动层智能定位实例代码 - Web前端
- jQuery EasyUI API 中文文档 - Draggable 可拖拽 - Web前端
- jQuery EasyUI API 中文文档 - Spinner微调器使用 - Web前端
- 基于jquery实现的鼠标拖拽元素复制并写入效果 - Web前端
- 很棒的学习jQuery的12个网站推荐 - Web前端
- 网页前端技术排行
- 最近发表
-
- WordPress随机显示特色图片插件:Random Post Thumbnails
- KeePass实现Chrome浏览器自动填充密码方法一
- LNMP一键包nginx 301强制跳转到https教程
- KeePass实现Chrome浏览器自动填充密码方法二
- #建站# 免费的VPS管理软件Xshell8/Xftp8中文版下载
- 使用Xshell 8连接VPS教程_电脑登录vps的方法
- WordPress评论界面添加烟花????效果
- 不同浏览器书签同步方案:坚果云+Floccus_详细使用教程
- iOS端KeePassXC客户端APP:Strongbox Password Safe
- 给WordPress评论中的Gravatar头像图片添加ALT属性


