17. Dynamic delays with animationend ⏰

In this example below you will see how to do a 17. Dynamic delays with animationend ⏰ with some HTML / CSS and Javascript

Demo for my guide to CSS animation.Creating dynamic delays with animationend.A Guide to CSS Animation - Part 1

Thumbnail
This awesome code was written by jh3y, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright jh3y ©
  • HTML
  • CSS
  • JavaScript
    <style>
  * {
    box-sizing: border-box;
  }
  html, body {
    align-items: center;
    background: linear-gradient(45deg, rebeccapurple, #dadfe1);
    display: flex;
    flex-direction: column;
    justify-content: center;
    margin: 0;
    min-height: 100vh;
    padding: 0;
    width: 100vw;
  }
  div {
    background: #f62459;
    height: 50px;
    margin: 40px;
    width: 50px;
  }
  h1 {
    color: #fafafa;
    font-family: 'Arial', sans-serif;
    font-size: 2rem;
    height: 2rem;
  }
</style>
<div class='animating'></div>
<h1></h1>

/*Downloaded from https://www.codeseek.co/jh3y/17-dynamic-delays-with-animationend-andx23f0-yEVBYG */
    .animating {
  animation: spin .5s ease-out;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}


/*Downloaded from https://www.codeseek.co/jh3y/17-dynamic-delays-with-animationend-andx23f0-yEVBYG */
    const spinner = document.querySelector('div')
const label = document.querySelector('h1')

const update = () => {
  spinner.classList.remove('animating')
  const interval = Math.floor(Math.random() * 5000)
  label.innerText = `Spinning in ${interval}ms`
  setTimeout(() => {
    spinner.classList.add('animating')
  }, interval)
}

spinner.addEventListener('animationend', update)

Comments