-
Notifications
You must be signed in to change notification settings - Fork 0
/
TopLoaderService.js
70 lines (54 loc) · 1.42 KB
/
TopLoaderService.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
const animationTime = 800;
const intervalTimeout = 10;
const widthVelocity = 110 * intervalTimeout / animationTime;
let targetReached = false;
let interval;
let loader;
let width = 0;
let target = 0;
const startInterval = () => {
if (!loader) {
loader = getLoader(document);
if (!loader) {
console.log('loader not found');
return;
}
}
if (!interval) {
interval = setInterval(animateWidth, intervalTimeout);
}
}
const animateWidth = () => {
if (width >= 100) {
clearTimeout(interval);
interval = null;
loader.style.width = 0;
width = 0;
return;
}
if (width >= target && !targetReached) {
targetReached= true;
}
width += !targetReached ? widthVelocity : (widthVelocity/50);
loader.style.width = width + '%';
}
export default class TopLoaderService {
static start(percentage) {
target = percentage || 35;
startInterval();
}
static end() {
target = 200;
targetReached = false;
}
}
const getLoader = (document) => {
let loader = document.getElementById("progress");
if (loader) return loader;
loader = document.createElement('div');
loader.setAttribute("id", "progress");
loader.appendChild(document.createElement('b'));
loader.appendChild(document.createElement('i'));
document.body.appendChild(loader);
return loader;
}