Jump to content
Search Community

How to use a variable within transform

pmascis 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

Hi all,

 

This is probably an easy Javascript question. Any help would greatly be appreciated.

I'm wanting to use a variable containing a measurement with transform: "translateX( VAR )" but am unsure of the syntax to get it working.

Currently, I have:
    var headerLeft = jQuery('#header .left');
    var headerLeftWidth = headerLeft.outerWidth();

And then ideally, I'd like to have something like the following:
.to(headerLeft, 1, {transform: "translateX(headerRightWidth)"});
But that as mentioned obviously doesn't work.

Console logging the variable works, and to get me out of trouble for the time being I'm using the following which works.
.to(headerLeft, 1, {x:headerLeftWidth});
But I'd like to learn the syntax of how to use within the quotes within the transform.

Thanks in advance!

Link to comment
Share on other sites

You're actually doing it in exactly the correct way. It's much better to just do x:headerLeftWidth than to do:

 

transform: "translateX(" + headerRightWidth + ")"

 

...because when you use the GSAP shortcuts like x, y, rotation, scaleX, scaleY, etc., it allows GSAP to avoid re-parsing the matrix from the browser, plus it's more accurate for any rotational values beyond 180 degrees (matrix decomposition is inherently ambiguous when it comes to rotational values). 

 

Happy tweening!

  • Like 1
  • Thanks 1
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...