Jump to content
Search Community

Bug with loosing vertical centering

Payman test
Moderator Tag

Go to solution Solved by GreenSock,

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

  • Solution

Yep, this is expected behavior, and while it may seem contrary at first, this is actually a strength of GSAP, not a weakness or bug. 

 

One of the inherent weaknesses of doing transforms in pure CSS is that all the components (x, y, scaleX, scaleY, rotation, skewX, skewY, etc.) are mashed into a single "transform" property. Thus, you cannot maintain the state of one component (like "y"/"translateY") while altering another (like "x") without a lot of headache, and it's impossible to ease them differently or offset timing. GSAP solves all of these problems, but it gets a little awkward when you try to mix both CSS and GSAP.

 

When GSAP gets the current state of the transform, the browser spits back a matrix() or matrix3d which is ALWAYS pixel-based (percentages are just translated into px at that point). So GSAP has no way of really knowing that you intended units to be %-based. This is why we always recommend that you set all your transform-related values through GSAP (at least on things that you plan to animate). This allows GSAP to properly discern intent with your units. In this case, just add this one line to the top of your JS: 

TweenLite.set(".box", {yPercent:-50});

Done.

 

Now GSAP knows that you want that value to be a percentage. Again, this isn't due to some bug in GSAP - it's just that the browser itself reports things always in px, and there's no way for GSAP to know otherwise. 

 

Flowing all your transform data through GSAP makes it much cleaner and more flexible. You can then very easily overlap easings/timings/whatever with any of the individual components, and you can look up their values simply using the _gsTransform object. Oh, and they'll work more consistently as well (like on SVG elements which won't work properly in many browsers if you try with CSS alone, or in an older browser like IE8). 

 

Does that answer your question? 

  • Like 4
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...