-
Notifications
You must be signed in to change notification settings - Fork 1
/
animateBarSequentially.js
48 lines (42 loc) · 1.31 KB
/
animateBarSequentially.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
const container = document.getElementById("container");
function animateBar(elementId, duration) {
return new Promise((resolve, reject) => {
let start = null;
const loadingBar = document.createElement("div");
loadingBar.classList.add("loadingBar");
loadingBar.id = elementId;
loadingBar.style.height = "100%";
loadingBar.style.backgroundColor = "blue";
loadingBar.style.width = "0";
container.appendChild(loadingBar);
function animate(timestamp) {
if (!start) {
start = timestamp;
}
const elapsedTime = timestamp - start;
const progress = Math.min((elapsedTime / duration) * 100, 100);
loadingBar.style.width = progress + "%";
if (elapsedTime < duration) {
requestAnimationFrame(animate);
} else {
resolve();
}
}
requestAnimationFrame(animate);
});
}
async function animateBarsSequentially() {
const barsConfig = [
{ id: "loadingBar1", duration: 3000 },
{ id: "loadingBar2", duration: 4000 },
{ id: "loadingBar3", duration: 5000 },
];
for (const { id, duration } of barsConfig) {
await animateBar(id, duration);
}
console.log("All animations complete");
}
// Start animating the bars sequentially
animateBarsSequentially().catch((error) =>
console.error("Animation error:", error)
);