forked from utags/userscripts
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogress-bar.ts
More file actions
53 lines (46 loc) · 1.14 KB
/
Copy pathprogress-bar.ts
File metadata and controls
53 lines (46 loc) · 1.14 KB
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
export class ProgressBar {
el: HTMLElement
timer: any
constructor() {
this.el = document.createElement('div')
this.el.style.cssText = `
position: fixed;
top: 0;
left: 0;
width: 0%;
height: 3px;
background: #0969da;
z-index: 2147483647;
transition: width 0.2s, opacity 0.2s;
opacity: 0;
pointer-events: none;
`
document.documentElement.append(this.el)
}
start() {
this.el.style.transition = 'width 0.2s, opacity 0.2s'
this.el.style.opacity = '1'
this.el.style.width = '0%'
// Force reflow
void this.el.getBoundingClientRect()
this.el.style.width = '30%'
if (this.timer) clearInterval(this.timer)
// Trickle
this.timer = setInterval(() => {
const w = Number.parseFloat(this.el.style.width) || 0
if (w < 90) {
this.el.style.width = w + (90 - w) * 0.1 + '%'
}
}, 200)
}
finish() {
if (this.timer) clearInterval(this.timer)
this.el.style.width = '100%'
setTimeout(() => {
this.el.style.opacity = '0'
setTimeout(() => {
this.el.style.width = '0%'
}, 200)
}, 200)
}
}