jquery图片预加载 等比缩放

 1 /*
2 * Image preload and auto zoom
3 * scaling 是否等比例自动缩放
4 * width 图片最大高
5 * height 图片最大宽
6 * loadpic 加载中的图片路径
7 * example $("*").LoadImage(true,w,h);
8 */
9 jQuery.fn.LoadImage=function(scaling,width,height,loadpic){
10 if(loadpic==null)loadpic="../images/loading.gif";
11 return this.each(function(){
12 var t=$(this);
13 var src=$(this).attr("src")
14 var img=new Image();
15 //alert("Loading...")
16 img.src=src;
17 //自动缩放图片
18 var autoScaling=function(){
19 if(scaling){
20
21 if(img.width>0 && img.height>0){
22 if(img.width/img.height>=width/height){
23 if(img.width>width){
24 t.width(width);
25 t.height((img.height*width)/img.width);
26 t.css("margin-top", (height-t.height())/2);
27 }else{
28 t.width(img.width);
29 t.height(img.height);
30 t.css("margin-top", (height-t.height())/2);
31 }
32 }
33 else{
34 if(img.height>height){
35 t.height(height);
36 t.width((img.width*height)/img.height);
37 t.css("margin-top", (height-t.height())/2);
38 }else{
39 t.width(img.width);
40 t.height(img.height);
41 t.css("margin-top", (height-t.height())/2);
42 }
43 }
44 }
45
46 }
47 }
48 //处理ff下会自动读取缓存图片
49 if(img.complete){
50 //alert("getToCache!");
51 autoScaling();
52 return;
53 }
54 $(this).attr("src","");
55 var loading=$("<img alt=\"加载中...\" title=\"图片加载中...\" src=\""+loadpic+"\" />");
56
57 t.hide();
58 t.after(loading);
59 $(img).load(function(){
60 autoScaling();
61 loading.remove();
62 t.attr("src",this.src);
63 t.show();
64 //alert("finally!")
65 });
66
67 });
68 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值