0

Is there any way to change the values of @keyframes (CSS) with jQuery

To make it a little bit more clear, I have this in my css file:

@keyframes bg {
  50% { background: #3b99fcff; }
}

.pie::before {
  content: '';
  position: absolute;
  top: 0; left: 50%;
  width: 50%; height: 100%;
  border-radius: 0 100% 100% 0 / 50%;
  background-color: inherit;
  transform-origin: left;
  animation: spin 50s linear infinite,
             bg 100s step-end infinite;
  animation-play-state: paused;
  animation-delay: inherit;
}

I would like to change the 'background' with Jquery

Ricardo
  • 1,308
  • 1
  • 10
  • 21

1 Answers1

2

You could use a CSS variable and change that:

setTimeout(() => {
  console.log("changing");
  document.getElementById("fizz").style.setProperty("--foo", "green");
}, 2000);
:root {
  --foo: blue;
}

@keyframes hello {
  from {
    background-color: yellow;
  }
  to {
    background-color: var(--foo);
  }
}

#fizz {
  height: 1em;
  animation: 1s infinite alternate hello;
}
<div id="fizz">
  <div>
zero298
  • 25,467
  • 10
  • 75
  • 100