Jump to content
Search Community

Unable to use css variables to animate to

md_t test
Moderator Tag

Warning: Please note

This thread was started before GSAP 3 was released. Some information, especially the syntax, may be out of date for GSAP 3. Please see the GSAP 3 migration guide and release notes for more information about how to update the code to GSAP 3's syntax. 

Recommended Posts

Hello everyone,

 

I have been using this awesome library for years to make amazing things and finally got a problem I can't seem to figure out on my own.

 

Whenever I put a value from a custom css property into TweenMax, it does not behave as expected. for example

 

var variableColor = getComputedStyle(document.documentElement).getPropertyValue('--color');

console.log(variableColor) // #f00

console.log(typeof variableColor) // String

 

TweenMax.to(el, 1, { backgroundColor: variableColor });

does not work and background will just turn black. In the code pen it also seems like it does not respect the initial background value from css.

 

el.style.backgroundColor = variableColor // does work.

 

var color = '#f00'

TweenMax.to(el, 1, { backgroundColor: color }); // does work as expected

 

I am not sure what I am doing wrong. This seems to work for other people as I read here.

 

https://greensock.com/forums/topic/21422-how-to-animate-using-css-variables-as-target-values/

 

See the Pen poowMzK?editors=1111 by m_truong (@m_truong) on CodePen

Link to comment
Share on other sites

Congrats on your first post, @md_t, especially after being a user for so long! 

 

It looks like the problem is that there's an extra space at the beginning of the value, so it's actually " #f00" instead of "#f00" :)

 

If you just add a .trim() at the end of it, that should fix the problem. Or in your CSS, make sure there's no space between the colon and the value, like --color:#f00

 

Happy tweening!

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...