forked from hilongjw/vue-lazyload
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vue-lazyload.es5.js
286 lines (245 loc) · 8.36 KB
/
vue-lazyload.es5.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
/*!
* Vue-Lazyload.js v0.9.2
* (c) 2016 Awe <hilongjw@gmail.com>
* Released under the MIT License.
*/
(function (global, factory) {
typeof exports === 'object' && typeof module !== 'undefined' ? module.exports = factory() :
typeof define === 'function' && define.amd ? define(factory) :
(global.install = factory());
}(this, (function () { 'use strict';
var inBrowser = typeof window !== 'undefined';
if (!Array.prototype.$remove) {
Array.prototype.$remove = function (item) {
if (!this.length) return;
var index = this.indexOf(item);
if (index > -1) {
return this.splice(index, 1);
}
};
}
var vueLazyload = (function (Vue) {
var Options = arguments.length <= 1 || arguments[1] === undefined ? {} : arguments[1];
var isVueNext = Vue.version.split('.')[0] === '2';
var DEFAULT_URL = 'data:image/gif;base64,R0lGODlhAQABAAAAACwAAAAAAQABAAA=';
var ListenEvents = ['scroll', 'wheel', 'mousewheel', 'resize', 'animationend', 'transitionend'];
var $Lazyload = {
listeners: {
loading: [],
loaded: [],
error: []
},
$on: function $on(event, func) {
this.listeners[event].push(func);
},
$once: function $once(event, func) {
var vm = this;
function on() {
vm.$off(event, on);
func.apply(vm, arguments);
}
this.$on(event, on);
},
$off: function $off(event, func) {
if (!func) {
this.listeners[event] = [];
return;
}
this.listeners[event].$remove(func);
},
$emit: function $emit(event, context) {
this.listeners[event].forEach(function (func) {
func(context);
});
}
};
var Init = {
preLoad: Options.preLoad || 1.3,
error: Options.error || DEFAULT_URL,
loading: Options.loading || DEFAULT_URL,
attempt: Options.attempt || 3,
scale: Options.scale || inBrowser ? window.devicePixelRatio : 1,
hasbind: false
};
var Listeners = [];
var imageCache = [];
var throttle = function throttle(action, delay) {
var timeout = null;
var lastRun = 0;
return function () {
if (timeout) {
return;
}
var elapsed = Date.now() - lastRun;
var context = this;
var args = arguments;
var runCallback = function runCallback() {
lastRun = Date.now();
timeout = false;
action.apply(context, args);
};
if (elapsed >= delay) {
runCallback();
} else {
timeout = setTimeout(runCallback, delay);
}
};
};
var _ = {
on: function on(el, type, func) {
el.addEventListener(type, func);
},
off: function off(el, type, func) {
el.removeEventListener(type, func);
}
};
var lazyLoadHandler = throttle(function () {
for (var i = 0, len = Listeners.length; i < len; ++i) {
checkCanShow(Listeners[i]);
}
}, 300);
var onListen = function onListen(el, start) {
if (start) {
ListenEvents.forEach(function (evt) {
_.on(el, evt, lazyLoadHandler);
});
} else {
Init.hasbind = false;
ListenEvents.forEach(function (evt) {
_.off(el, evt, lazyLoadHandler);
});
}
};
var checkCanShow = function checkCanShow(listener) {
if (imageCache.indexOf(listener.src) !== -1) return setElRender(listener.el, listener.bindType, listener.src, 'loaded');
var rect = listener.el.getBoundingClientRect();
if (rect.top < window.innerHeight * Init.preLoad && rect.bottom > 0 && rect.left < window.innerWidth * Init.preLoad && rect.right > 0) {
render(listener);
}
};
var setElRender = function setElRender(el, bindType, src, state, context) {
if (!bindType) {
el.setAttribute('src', src);
} else {
el.setAttribute('style', bindType + ': url(' + src + ')');
}
el.setAttribute('lazy', state);
if (context) {
$Lazyload.$emit(state, context);
}
};
var render = function render(item) {
if (item.attempt >= Init.attempt) return false;
item.attempt++;
if (imageCache.indexOf(item.src) !== -1) return setElRender(item.el, item.bindType, item.src, 'loaded');
imageCache.push(item.src);
loadImageAsync(item, function (image) {
setElRender(item.el, item.bindType, item.src, 'loaded', item);
Listeners.$remove(item);
}, function (error) {
imageCache.$remove(item.src);
setElRender(item.el, item.bindType, item.error, 'error', item);
});
};
var loadImageAsync = function loadImageAsync(item, resolve, reject) {
var image = new Image();
image.src = item.src;
image.onload = function () {
resolve({
naturalHeight: image.naturalHeight,
naturalWidth: image.naturalWidth,
src: item.src
});
};
image.onerror = function (e) {
reject(e);
};
};
var componentWillUnmount = function componentWillUnmount(el, binding, vnode, OldVnode) {
if (!el) return;
for (var i = 0, len = Listeners.length; i < len; i++) {
if (Listeners[i] && Listeners[i].el === el) {
Listeners.splice(i, 1);
}
}
if (Init.hasbind && Listeners.length == 0) {
onListen(window, false);
}
};
var checkElExist = function checkElExist(el) {
var hasIt = false;
Listeners.forEach(function (item) {
if (item.el === el) hasIt = true;
});
if (hasIt) {
return Vue.nextTick(function () {
lazyLoadHandler();
});
}
return hasIt;
};
var addListener = function addListener(el, binding, vnode) {
if (el.getAttribute('lazy') === 'loaded') return;
if (checkElExist(el)) return;
var parentEl = null;
var imageSrc = binding.value;
var imageLoading = Init.loading;
var imageError = Init.error;
if (binding.value && typeof binding.value !== 'string') {
imageSrc = binding.value.src;
imageLoading = binding.value.loading || Init.loading;
imageError = binding.value.error || Init.error;
}
if (imageCache.indexOf(imageSrc) > -1) return setElRender(el, binding.arg, imageSrc, 'loaded');
Vue.nextTick(function () {
if (binding.modifiers) {
parentEl = window.document.getElementById(Object.keys(binding.modifiers)[0]);
}
var listener = {
bindType: binding.arg,
attempt: 0,
parentEl: parentEl,
el: el,
error: imageError,
src: imageSrc
};
Listeners.push(listener);
setElRender(el, binding.arg, imageLoading, 'loading', listener);
lazyLoadHandler();
if (Listeners.length > 0 && !Init.hasbind) {
Init.hasbind = true;
onListen(window, true);
if (parentEl) {
onListen(parentEl, true);
}
}
});
};
Vue.prototype.$Lazyload = $Lazyload;
if (isVueNext) {
Vue.directive('lazy', {
bind: addListener,
update: addListener,
inserted: addListener,
componentUpdated: lazyLoadHandler,
unbind: componentWillUnmount
});
} else {
Vue.directive('lazy', {
bind: lazyLoadHandler,
update: function update(newValue, oldValue) {
addListener(this.el, {
modifiers: this.modifiers,
arg: this.arg,
value: newValue,
oldValue: oldValue
});
},
unbind: function unbind() {
componentWillUnmount(this.el);
}
});
}
});
return vueLazyload;
})));