-
Notifications
You must be signed in to change notification settings - Fork 0
/
lazyload.js
66 lines (51 loc) · 1.44 KB
/
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
/**
* Created by alejandrorioscalera
* On 24/8/18
*
* -- SOCIAL NETWORKS --
*
* GitHub: https://github.com/clonalejandro or @clonalejandro
* Website: https://clonalejandro.me/
* Twitter: https://twitter.com/clonalejandro11/ or @clonalejandro11
* Keybase: https://keybase.io/clonalejandro/
*
* -- LICENSE --
*
* All rights reserved for clonalejandro ©lazyload 2018 / 2019
*/
class LazyLoad {
/** SMALL CONSTRUCTORS **/
constructor(timeout = 50){
setTimeout(() => {
this.buildImages().forEach(element => {
const source = element.getAttribute("data-src");
element.setAttribute("data-src", null);
element.setAttribute("src", source);
})
}, timeout)
}
/** REST **/
/**
* This function initialize the images var
*/
buildImages(){
let generalImages = document.getElementsByTagName("img");
let array = new Array();
Object.keys(generalImages).forEach(key => {
const element = generalImages[key];
if ( !this.isNull( element.getAttribute("data-src") ) )
array.push(element);
});
return array
}
/**
* This function check if the data is null
* @param {*} obj
* @return {boolean} isNull
*/
isNull(obj){
return obj == null ||
obj == undefined ||
obj == ""
}
}