-
Notifications
You must be signed in to change notification settings - Fork 1
/
loadingBarWithoutRAF.js
53 lines (42 loc) · 1.43 KB
/
loadingBarWithoutRAF.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
let container = document.getElementsByClassName("container")?.[0];
let btn = document.getElementsByClassName("btn")?.[0];
let progressQueue = [];
let isAnimating = false;
btn.addEventListener("click", startLoadingBar);
function startLoadingBar() {
progressQueue.push(3);
processQueue();
}
function createLoadingBar(duration) {
// Create a div
const loadingBar = document.createElement("div");
loadingBar.classList.add("loadingBar");
// Append the div to the DOM
container.appendChild(loadingBar);
// Set the dynamic transition duration
loadingBar.style.transitionDuration = duration + "s";
loadingBar.style.backgroundColor = "red";
// Start the animation after a short delay to ensure the element is in the DOM
setTimeout(() => {
loadingBar.style.width = "100%";
}, 10); // Small delay to ensure smooth transition
// Add an event listener for when the transition ends
loadingBar.addEventListener("transitionend", onTransitionEnd);
}
function onTransitionEnd(event) {
// Remove the event listener to avoid multiple triggers
event.target.removeEventListener("transitionend", onTransitionEnd);
// Reset the flag and process the next item in the queue
if (isAnimating) {
isAnimating = false;
processQueue();
}
}
function processQueue() {
if (isAnimating || progressQueue.length === 0) {
return;
}
isAnimating = true;
const duration = progressQueue.shift();
createLoadingBar(duration);
}