最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

html - How to trigger a css animation with vanilla javascript - Stack Overflow

programmeradmin1浏览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;} 
}

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;} 
}
Share Improve this question asked Nov 23, 2020 at 20:24 Max McCarthyMax McCarthy 501 gold badge4 silver badges10 bronze badges 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 – Calvin Nunes 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/en-US/docs/Web/API/Element/animate – arieljuod Commented Nov 23, 2020 at 20:26
Add a ment  | 

4 Answers 4

Reset to default 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>

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!

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>

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>

发布评论

评论列表(0)

  1. 暂无评论