-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
51 lines (45 loc) · 951 Bytes
/
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
/**
* Skeletonize.js
* Dynamic skeleton loaders with minimal effort
*/
export default class Skeletonize {
constructor(node, options) {
this.node = node;
this.skeletonEls = [];
this.init();
}
init = () => {
this._walkDOM(this.node);
}
add = () => {
this.skeletonEls.map(el => {
el.style.position = "relative";
el.classList.add('skeletonizejs');
})
}
remove = () => {
this.skeletonEls.map(el => {
el.style.position = "";
el.classList.remove('skeletonizejs');
})
}
/**
* @private
* walkDom
* Recursive get and store nodes
* Adapted from Javascript: the good parts
*/
_walkDOM = (node) => {
this._filter(node);
node = node.firstElementChild;
while(node) {
this._walkDOM(node, this._filter);
node = node.nextElementSibling;
}
};
_filter = (node) => {
if(node.children.length === 0){
this.skeletonEls.push(node)
}
}
}