forked from liulangnan/aui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaui-lazyload.js
48 lines (48 loc) · 1.64 KB
/
aui-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
/**
* aui-lazyload.js
* @author 流浪男
* Licensed under the MIT license.
* http://www.opensource.org/licenses/mit-license.php
*/
(function( window, undefined ) {
"use strict";
var _loadImgNodes;
var auiLazyload = function(params) {
this.errorImage = params.errorImage||false;
this._init(params);
};
auiLazyload.prototype = {
_init: function(params) {
var self = this;
_loadImgNodes = document.querySelectorAll('[data-src]');
self._judgeImages();
window.addEventListener('scroll', function(){
_loadImgNodes = document.querySelectorAll('[data-src]');
self._judgeImages();
}, false);
},
_judgeImages:function() {
var self = this;
if(_loadImgNodes.length){
for(var i = 0; i < _loadImgNodes.length; i++){
if (_loadImgNodes[i].getBoundingClientRect().top < window.innerHeight) {
self._loadImage(_loadImgNodes[i]);
}
}
}
},
_loadImage:function(el){
var self = this;
var img = new Image();
img.src = el.getAttribute('data-src');
el.src = el.getAttribute('data-src');
el.removeAttribute("data-src");
// // 图片加载失败
img.onerror = function() {
el.src = self.errorImage || el.getAttribute('src');
el.removeAttribute("data-src");
};
}
}
window.auiLazyload = auiLazyload;
})(window);