-
Notifications
You must be signed in to change notification settings - Fork 0
/
jquery.lazyload.js
167 lines (146 loc) · 5.89 KB
/
jquery.lazyload.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/**
* @author littenli
* @date 2014-03-10 version 0.2
* @description 图片延时加载,裂图替换,图片错误上报处理
* @update 增加非可视区域延时加载
* @example $(".container").lazy(options);
* 遍历$(".container")节点内的img节点,都应用lazyload;若此节点为img节点,只应用此节点
* options.srcSign {String} 可为空.img节点约定的src标志,默认为lazy-src;响应img节点为:<img lazy-src="img/hello.jpg" />
* options.errCallBack {Function} 可为空.提供img加载失败回调,供业务额外去处理加载失败逻辑
* options.container {Dom} 提供容器节点内可视区域的加载能力,默认为window
*/
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['jquery'], factory);
} else {
factory(root['jQuery']);
}
}(this, function ($) {
$.fn.lazyload = function(options) {
return this.each(function() {
options = options || {};
var defualts = {};
var opts = $.extend({}, defualts, options);
var obj = $(this);
var dom = this;
var srcSign = options.srcSign || "lazy-src";
var errCallBack = options.errCallBack || function(){};
var container = options.container || $(window);
/**
* @description src正常
*/
var imgload = function (e, target) {
//todo: 上报
}
/**
* @description src失效
*/
var imgerr = function (e, target, fn, src) {
if(target[0].src && (target[0].src.indexOf("img-err.png")>0 || target[0].src.indexOf("img-err2.png")>0)){
return ;
}
var w = target.width();
var h = target.height();
target[0].src = "/img/img-err.png";
fn();
//todo: 上报
};
var tempImg = function(target){
var w = target.width();
var h = target.height();
var t = target.offset().top;
var l = target.offset().left;
var tempDom = target.clone().addClass("lazy-loding").insertBefore(target);
tempDom[0].src = "/img/img-loading.png";
target.hide();
}
/**
* @description src替换,loading过程中添加类lazy-loading;
*/
var setSrc = function(target, srcSign, errCallBack){
if(target.attr("src"))return ;
if(options.cache == true){
console.log(target);
//存进localstorage
var canvas1 = document.getElementById('canvas1');
var ctx1 = canvas1.getContext('2d');
var imageData;
image = new Image();
image.src = target.attr(srcSign);
image.onload=function(){
ctx1.drawImage(image,0,0);
imageData = ctx1.getImageData(0,0,500,250);
console.log(imageData);
}
}else{
tempImg(target);
var src = target.attr(srcSign);
target[0].onerror = function (e) {
imgerr(e, target, errCallBack, src);
};
target[0].onload = function (e) {
target.parent().find(".lazy-loding").remove();
target.show();
imgload(e, target);
}
target[0].src = src;
}
}
/**
* @description 重组
*/
opts.cache = [];
if(dom.tagName == "IMG"){
var data = {
obj: obj,
tag: "img",
url: obj.attr(srcSign)
};
opts.cache.push(data);
}else{
var imgArr = obj.find("img");
imgArr.each(function(index) {
var node = this.nodeName.toLowerCase(), url = $(this).attr(srcSign);
//重组
var data = {
obj: imgArr.eq(index),
tag: node,
url: url
};
opts.cache.push(data);
});
}
//动态显示数据
var scrollHandle = function() {
var contHeight = container.height();
var contop;
if ($(window).get(0) === window) {
contop = $(window).scrollTop();
} else {
contop = container.offset().top;
}
$.each(opts.cache, function(i, data) {
var o = data.obj, tag = data.tag, url = data.url, post, posb;
if (o) {
post = o.offset().top - contop, post + o.height();
if ((post >= 0 && post < contHeight) || (posb > 0 && posb <= contHeight)) {
if (url) {
//在浏览器窗口内
if (tag === "img") {
//改变src
setSrc(o, srcSign, errCallBack);
}
}
data.obj = null;
}
}
});
}
//加载完毕即执行
scrollHandle();
//滚动执行
container.bind("scroll", scrollHandle);
container.bind("resize", scrollHandle);
});
};
}));