-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathindex.js
161 lines (139 loc) · 4.14 KB
/
index.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
/**
* MIT License
* @author Barret Lee<http://barretlee.com/about/>
* @datetime 2015-11-16 20:21:27
*/
~function(window, undefined) {
// exports to global
umd("Lazyload", Lazyload);
Lazyload.SENCER = 30;
var noop = function() {};
// Lazyload Component
function Lazyload(elements, opts) {
var self = this;
this.tag = "data-src";
this.distance = 0;
this.callback = noop;
this._pause = false;
// mixin
var opts = opts || {};
for(var key in opts) {
this[key] = opts[key];
}
this.elements = typeof elements === "string" ? $(elements) : elements;
setTimeout(function(){
self.init();
}, 4);
};
// init, bind event
Lazyload.prototype.init = function() {
var self = this;
self._detectElementIfInScreen();
var timer;
addEventListener("scroll", function() {
timer && clearTimeout(timer);
timer = setTimeout(function() {
self._detectElementIfInScreen();
}, Lazyload.SENCER);
});
addEventListener("resize", function(){
timer && clearTimeout(timer);
self._detectElementIfInScreen();
});
};
// detect if in screen
Lazyload.prototype._detectElementIfInScreen = function() {
if(!this.elements.length || this._pause) return;
var W = window.innerWidth || document.documentElement.clientWidth;
var H = window.innerHeight || document.documentElement.clientHeight;
for (var i = 0, len = this.elements.length; i < len; i++) {
var ele = this.elements[i];
var rect = ele.getBoundingClientRect();
if((rect.top >= this.distance && rect.left >= this.distance
|| rect.top < 0 && (rect.top + rect.height) >= this.distance
|| rect.left < 0 && (rect.left + rect.width >= this.distance))
&& rect.top <= H && rect.left <= W ){
this.loadItem(ele);
this.elements.splice(i, 1);
i--; len--;
}
}
if(!this.elements.length) {
this.callback && this.callback();
}
};
Lazyload.prototype.pause = function() {
this._pause = true;
return this;
};
Lazyload.prototype.restart = function() {
this._pause = false;
this._detectElementIfInScreen();
return this;
};
// lazyload img or script
Lazyload.prototype.loadItem = function(ele) {
var imgs = ele.getElementsByTagName("img");
for(var i = 0, len = imgs.length; i < len; i++) {
var img = imgs[i];
var src = img.getAttribute(this.tag);
if(src) {
img.setAttribute("src", src);
}
}
var textareas = ele.getElementsByTagName("textarea");
for(var j = 0, len = textareas.length; j < len; j++){
var script = textareas[j].value;
if(window.execScript) {
window.execScript(script);
} else {
new Function(script)();
}
}
};
// mini Query
function $(query) {
var res = [];
if (document.querySelectorAll) {
res = document.querySelectorAll(query);
} else {
var firstStyleSheet = document.styleSheets[0] || document.createStyleSheet();
firstStyleSheet.addRule(query, 'Barret:Lee');
for (var i = 0, len = document.all.length; i < len; i++) {
var item = document.all[i];
item.currentStyle.Barret && res.push(item);
}
firstStyleSheet.removeRule(0);
}
if(res.item) { /* Fuck IE8 */
var ret = [];
for(var i = 0, len = res.length; i < len; i++){
ret.push(res.item(i));
}
res = ret;
}
return res;
};
function addEventListener(evt, fn){
window.addEventListener ? this.addEventListener(evt, fn, false) : (window.attachEvent)
? this.attachEvent('on' + evt, fn) : this['on' + evt] = fn;
}
// UMD
function umd(name, component) {
switch (true) {
case typeof module === 'object' && !!module.exports:
module.exports = component;
break;
case typeof define === 'function' && !!define.amd:
define(name, function() {
return component;
});
break;
default:
try { /* Fuck IE8- */
if (typeof execScript === 'object') execScript('var ' + name);
} catch (error) {}
window[name] = component;
}
};
}(window, void 0);