0

Im trying to use javascript's animate() function to animate a custom CSS variable (ex. "--foo-var: red")

foo.animate(
  {
    --foo-bar: `blue`,
  },
  { duration: 200, fill: "forwards" }
);
8
  • For starters, --foo-bar as a property name can not work in this syntax - you'd have to put it in quotes. Not sure if you will be able to directly animate this then though - you might need to declare your variable as a custom property first, stackoverflow.com/a/64333786/1427878 Commented Jun 18, 2024 at 10:34
  • Also I believe that CSS variables are only evaluated once at the start of the page build. Changing the variable later has no effect (as far I as I understand). Commented Jun 18, 2024 at 10:36
  • @Kokodoko I sincerely hope thats not the case because that would break quite a lot. It is only evaluated once at the start of an animation in my experience (so changing it does not really affect the animation if its running) but otherwise they should definitely update. Otherwise variables would be pretty... useless. Commented Jun 18, 2024 at 10:56
  • You are correct :) I did not know that :) But I was able to hack it by setting the animation as a string: foo.style.animation = "my-animation 2s forwards" and then keeping the actual keyframe animation in the stylesheet: jsfiddle.net/3roed9qv Commented Jun 18, 2024 at 12:53
  • @CBroe the link you have provided is a CSS solution to my question. I was hopping that it would be possible to do it in Javascript some how. Commented Jun 19, 2024 at 7:51

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.