-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
55 lines (37 loc) · 1.39 KB
/
script.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
var progressBars = document.querySelectorAll(".skill-progress > div");
function initialiseBar(bar) {
bar.setAttribute("data-visited", false);
bar.style.width = 0 + '%';
}
for (var bar of progressBars) {
initialiseBar(bar);
}
function fillBar(bar) {
var currentWidth = 0;
var targetWidth = bar.getAttribute("data-bar-width");
var interval = setInterval(function () {
if (currentWidth >= targetWidth) {
clearInterval(interval);
return;
}
currentWidth++;
bar.style.width = currentWidth + '%';
}, 5);
}
// This function uses a for loop for individual progress bars.
function checkScroll() {
for (let bar of progressBars) {
var barCoordinates = bar.getBoundingClientRect();
if ((bar.getAttribute("data-visited") == "false") &&
(barCoordinates.top <= (window.innerHeight - barCoordinates.height))) {
bar.setAttribute("data-visited", true);
fillBar(bar);
} else if (barCoordinates.top > window.innerHeight) {
bar.setAttribute("data-visited", false);
initialiseBar(bar);
}
}
}
window.addEventListener("scroll", checkScroll);
// This event fills the progress bars if they are displayed on the screen when the page is loaded.
//window.addEventListener("load", checkScroll);