0

I'm trying to make a css animation trigger with javascript.I have made the animation with @keyframes and it works. Dose anyone know how to do this without stuff like jQuery?

Here is my code:

/* Animations */

@keyframes party{
    0% {background-color: red;}
    10% {background-color: orange;}
    20% {background-color: yellow;}
    30% {background-color: green;}
    40% {background-color: blue;}
    50% {background-color: purple;} 
}

@-webkit-keyframes party{
    0% {background-color: red;}
    10% {background-color: orange;}
    20% {background-color: yellow;}
    30% {background-color: green;}
    40% {background-color: blue;}
    50% {background-color: purple;} 
}
2
  • 3
    you need to create another CSS that uses your keyframe animation, like .animated {animation: party 2s infinite} and then with JS you add this class to the element you want to animate Commented Nov 23, 2020 at 20:25
  • You can add a class that adds the animation property and there's also an animate method for html elements to animate some stuff developer.mozilla.org/en-US/docs/Web/API/Element/animate Commented Nov 23, 2020 at 20:26

4 Answers 4

4

I think adding class is a healthy way to do it.

Check the example here: https://codepen.io/yasgo/pen/zYBgjXN

document.getElementById('box').classList.add('active-animation');
.box {
  width: 50px;
  height: 50px;
  background-color: black;
}

.box.active-animation {
  animation: party 5s infinite;
}

@keyframes party{
    0% {background-color: red;}
    10% {background-color: orange;}
    20% {background-color: yellow;}
    30% {background-color: green;}
    40% {background-color: blue;}
    50% {background-color: purple;} 
}
<div id="box" class="box"></di>

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

Comments

2

It's a lot easier than you think, you only need to call the animation, like this:

This is a example to trigger with a button

function party(){
  document.body.style.animation = 'party 2.5s infinite linear';
}
body{
background-color: purple;
}

@keyframes party{
    0% {background-color: red;}
    10% {background-color: orange;}
    20% {background-color: yellow;}
    30% {background-color: green;}
    40% {background-color: blue;}
    50% {background-color: purple;} 
}

@-webkit-keyframes party{
    0% {background-color: red;}
    10% {background-color: orange;}
    20% {background-color: yellow;}
    30% {background-color: green;}
    40% {background-color: blue;}
    50% {background-color: purple;} 
}
<html>
  <body id="bd">
    <button onclick="party()">Press Me!</button>
  </body>


</html>

I wait this could help you!

Comments

0

var box = document.getElementById('box');
box.style.animation = "party 5s infinite";
#box {
  width: 200px;
  height:150px;
  background-color: black;
  margin: 20px auto;
  border-radius: 5px;
}

@keyframes party{
    0% {background-color: red;}
    10% {background-color: orange;}
    20% {background-color: yellow;}
    30% {background-color: green;}
    40% {background-color: blue;}
    50% {background-color: purple;} 
}

@-webkit-keyframes party{
    0% {background-color: red;}
    10% {background-color: orange;}
    20% {background-color: yellow;}
    30% {background-color: green;}
    40% {background-color: blue;}
    50% {background-color: purple;} 
}
<div id="box"></div>

Comments

0

You could set the animation property of the element you are trying to animate, like this.

// sets the css animation property
const spinner = document.getElementById("spinner");
spinner.style.animation = "spin 3s linear infinite";
/* gives the div element a border and size */
#spinner {
  border: 15px solid black;
  border-top: 15px solid white;
  border-bottom: 15px solid white;
  border-radius: 50%;
  width: 50px;
  height: 50px;
  /* no animation property is inserted here */
}

/* spin animation */
@keyframes spin {
  0% {
    transform: rotate(0deg);
  }
  100% {
    transform: rotate(360deg);
  }
}
<!-- div element to animate -->
<div id="spinner"></div>

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.