-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathlazyelem.js
289 lines (241 loc) · 7.95 KB
/
lazyelem.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*!
* lazyelem - lazyloader for html elements
* Author: LiXiao
* Released under MIT license
*/
;(function(window, $) {
'use strict';
var $win = $(window),
// 滚动监听是否开启
isListening = false,
// 被监听的元素集合
listeners = [],
groups = {},
// 回调函数集合
callbacks = [],
// 默认配置
config = {
timeout: 10,
buffer: 100,
loadingClass: 'lazy-loading',
srcValue: 'lazy-src',
bgValue: 'lazy-bg'
},
that, timer;
/**
* 全局主对象
* @module lazyelem
*/
var lazyelem = {
/**
* 初始化框架
* @method _init
*/
_init: function() {
that = this;
},
/**
* 添加监听元素
* @method listen
* @param {String|Object} selector 用于选中元素的jQuery选择器,或直接传入一组jQuery对象
* @param {String} type 元素类型,可用值 'img'|'bg'|dom'|'fn'
* @param {Function} callback 加载后的回调函数 参数1:当前触发加载条件的jquery对象
*/
listen: function(selector, type, callback) {
var elements = selector,
eType = (type || 'img').toLowerCase(),
cbIndex;
// 判断传入的是否选择器
if ((typeof selector).toLowerCase() != 'object') {
elements = $(selector || 'img[' + config.srcValue + ']');
}
// 注册回调函数
if (callback) {
cbIndex = callbacks.push(callback) - 1;
}
// 添加批处理组
if (eType === 'bat') {
groups[cbIndex] = {
objs: [],
cbIndex: cbIndex
};
}
// 添加监听元素
elements.each(function() {
var o = $(this);
listeners.push({
type: eType,
obj: o,
cbIndex: cbIndex
});
// 加上loading class
if (eType == 'img') {
o.addClass(config.loadingClass);
}
});
// 启动窗口滚动事件监听
if (!isListening) {
that._startListen();
}
that.detect();
},
/**
* 绑定窗口监听事件
* @method _startListen
*/
_startListen: function() {
$win.bind('scroll.lazyelem resize.lazyelem', function() {
if (!listeners.length) {
return;
}
clearTimeout(timer);
timer = setTimeout(function() {
that.detect();
}, config.timeout);
});
isListening = true;
},
/**
* 检测所有元素是否满足加载条件
* @method detect
*/
detect: function() {
for (var i = 0; i < listeners.length; i++) {
var listener = listeners[i],
obj = listener.obj,
cbIndex = listener.cbIndex;
// 以下情况直接跳过本次循环: 元素不可见 | 不在屏幕区域内
if (that._isHidden(obj) || !that._isTrigger(obj)) {
continue;
}
// 开始加载
switch(listener.type) {
case 'fn':
break;
case 'img':
var src = obj.attr(config.srcValue);
src && obj.attr('src', src).removeAttr(config.srcValue);
break;
case 'bg':
var bg = obj.attr(config.bgValue);
bg && obj.css('background-image', 'url(' + bg + ')').removeAttr(config.bgValue);
break;
case 'dom':
var script = obj.children('script');
script.length && script.replaceWith(that._minHtml(script.html()));
break;
case 'bat':
groups[cbIndex].objs.push(obj);
break;
}
// 删除该项
listeners.splice(i--, 1);
// 调用回调函数
if (listener.type !== 'bat' && cbIndex >= 0) {
callbacks[cbIndex](obj);
}
}
// 批处理
that._bat();
// 当所有元素加载完毕,停止监听
if (listeners.length === 0) {
$win.unbind('scroll.lazyelem resize.lazyelem');
isListening = false;
}
},
/**
* 批量处理懒加载元素
* @method _bat
*/
_bat: function() {
for (var g in groups) {
var objs, cb;
if (groups.hasOwnProperty(g)) {
objs = groups[g].objs;
cb = callbacks[groups[g].cbIndex];
if (objs.length) {
cb(objs);
groups[g].objs = [];
}
}
}
},
/**
* 判断一个jquery对象是否在屏幕区域之间
* @method _isTrigger
* @param {jQuery Object} obj 单个jquery对象
* @return {Boolean}
*/
_isTrigger: function(obj) {
var winHeight = $win.height(),
winTop = $win.scrollTop(),
oHeight = obj.height(),
ot = obj.offset().top;
return (ot + oHeight) > (winTop - config.buffer) && ot < (winTop + winHeight + config.buffer);
},
/**
* 判断一个jquery对象是否可见
* @method _isHidden
* @param {jQuery Object} obj 单个jquery对象
* @return {Boolean}
*/
_isHidden: function(obj) {
var elem = obj[0];
if (obj.css('display') === 'none' || !$.contains(elem.ownerDocument, elem )) {
return true;
}
if (elem.offsetWidth <= 0 && elem.offsetHeight <= 0) {
return true;
}
return false;
},
/**
* 最小化一段html文本
* @method _minHtml
* @param {String} html 一段html代码文本
* @return {String} 处理后的html文本
*/
_minHtml: function(html) {
var rep = /\n+/g,
repone = /<!--.*?-->/ig,
reptwo = /\/\*.*?\*\//ig,
reptree = /[ ]+</ig;
html = html.replace(rep, '');
html = html.replace(repone, '');
html = html.replace(reptwo, '');
html = html.replace(reptree, '<');
return html;
},
/**
* 配置参数
* @method config
* @param {Object} opt 配置项 timeout|buffer|loadingClass|srcValue|bgValue
*/
config: function(opt) {
$.extend(config, opt);
},
/**
* 清除监听对象
* @method clear
* @param {Object} elem 指定要清除的DOM对象
*/
clear: function(elem) {
if (elem) {
for (var i = 0; i < listeners.length; i++) {
if (elem === listeners[i].obj[0]) {
listeners.splice(i, 1);
}
}
}
else {
listeners = [];
groups = {};
callbacks = [];
}
}
};
// 初始化
lazyelem._init();
// 附加到全局对象
window.lazyelem = lazyelem;
})(window, window.jQuery || window.Zepto);