JavaScript Progress Bar

How to Create an Animated Progress Bar with Vanilla JavaScript

html

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <script src="./app.js" defer></script>
    <link rel="stylesheet" href="./style.css">
    <title>Document</title>
</head>

<body>
    <div id="progressBar"></div>
</body>

</html>

css

* {
    padding: 0;
    margin: 0;
    box-sizing: border-box;
}

#progressBar {
    position: fixed;
    top: 0;
    left: 0;
    height: 3px;
    width: 0;
    background-color: #894aff;
    border-radius: 0 50px 50px 0;
    transition: width 0.4s ease;
    z-index: 9999;
}

javascript

const progressBar = document.getElementById('progressBar');

let width = 0;
let interval = setInterval(() => {
    if (width < 90) {
        width += 1;
        progressBar.style.width = width + "%";
    }
}, 20);

window.addEventListener('load', () => {
    clearInterval(interval);
    progressBar.style.width = "100%";

    progressBar.addEventListener('transitionend', () => {
        progressBar.style.opacity = "0";
        setTimeout(() => {
            progressBar.remove();
        }, 300);
    }, { once: true });
});