JSFiddle - React, Tailwind, and code Playground

by princeofabyss

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.1/jquery.min.js"></script>
<div id='progressbar'></div>

CSS

.progressbar {
  width: calc(100% - 50px);
  border: solid 4px lightgray;
  border-radius: 20px;
  background-color: #fff;
  margin: 0 auto;
}

.progressbar .inner {
  height: 25px;
  border-radius: 20px;
  animation: progressbar-countdown;
  animation-iteration-count: 1;
  animation-fill-mode: forwards;
  animation-play-state: paused;
  animation-timing-function: linear;
}

@keyframes progressbar-countdown {
  0% {
    width: 100%;
    background: blue;
  }

  100% {
    width: 0%;
    background: red;
  }
}

.bars {
  width: 100%;
  height: 25px;
  animation: progress-bar-stripes 0.7s linear infinite;
  background-image: linear-gradient(45deg, rgba(255, 255, 255, 0.15) 25%, transparent 25%, transparent 50%, rgba(255, 255, 255, 0.15) 50%, rgba(255, 255, 255, 0.15) 75%, transparent 75%, transparent);
  background-size: 1rem 1rem;
}

@keyframes progress-bar-stripes {
  0% {
    background-position: 0 0;
  }

  100% {
    background-position: 2rem 0;
  }
}

JavaScript

function createProgressbar(id, duration, delay, callback) {
  var progressbar = document.getElementById(id);
  progressbar.className = 'progressbar';

  var progressbarinner = document.createElement('div');
  progressbarinner.className = 'inner';

  var bars = document.createElement('div');
  bars.className = 'bars';

  progressbarinner.style.animationDuration = duration;
  progressbarinner.style.animationDelay = delay;

  if (typeof(callback) === 'function') {
    progressbarinner.addEventListener('animationend', callback);
  }

  progressbar.appendChild(progressbarinner);
  progressbarinner.appendChild(bars);

  progressbarinner.style.animationPlayState = 'running';
}

$(window).on('load', function() {
  createProgressbar('progressbar', '120s', '-' + Math.floor(Math.random() * 10) + 1 + 's', function() {
    alert('finished');
  });
});