1

I've been searching all over the internet but couldn't find an answer to this.

Basically, I want to make an if statement in javascript... Once a keyframes animation in CSS3 gets to 100%, do some action. I just don't know what syntax to use.

I really appreciate any help you can provide.

CSS3:

@keyframes progress-animation{
0%{
    width: 0%;
}

100%{
    width: 100%;
}
}

@keyframes loader {
0% {
  transform: rotate(0deg);
}

25% {
  transform: rotate(180deg);
}

50% {
  transform: rotate(180deg);
}

75% {
  transform: rotate(360deg);
}

100% {
  transform: rotate(360deg);
}

}

What I want to do (in words):

if (progress-animation == '100%')
    stop animation (loader)

Sorry if it's a quite simple answer; I'm a bit new to Javascript.

3
  • 2
    Take a look at animationend event. w3schools.com/jsref/event_animationend.asp Commented May 16, 2021 at 23:22
  • 1
    There should be no need to stop the animation really. Perhaps you can add the CSS this progress-animation is living in? It should stop once it reaches 100% of the duration you have set, unless you have it set to infinte. Commented May 16, 2021 at 23:24
  • Hey sorry, there was a typo, I meant to stop another keyframes animation, not the same one. Just updated, Thanks! Commented May 16, 2021 at 23:50

2 Answers 2

3

You can utilize animationend event.

You can also add a condition using animationName parameter received from the event to only check for particular animations.

Here's a demo:

const element = document.querySelector('.progress')
element.addEventListener("animationend", onAnimationComplete);

function onAnimationComplete(e){
  if(e.animationName==='progress-animation'){
     console.log("progress-animation ended")
  }
}
.progress{
  background:red;
  padding:10px;
  width:100px;
  border:solid 1px black;
  border-radius:16px;
  animation: progress-animation linear 3s;
}
@keyframes progress-animation{
  0%{
  width:0px;
  }
  100%{
   width:100px;
 }
}
<div class='progress'>

</div>

Sign up to request clarification or add additional context in comments.

Comments

0

Using This guide

you could use something like the below:

let style = document.getElementById('WhateverElementIsCalled').style;
if(style[progress-animation] === '100%') {
    //Do a thing
} 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.