Jump to content
Search Community

Tweening "From" x, then resizing

Korben Dallas 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 guys,

 

I have a problem with a "h1", I have centered it with the famous :

 

position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);

 

So it's perfectly centered, then I want to make it appear from the right so i'm tweening it like this

   

TweenMax.from("h1", 1, { opacity: 0, x:200})

 

The animation run fine, but if we resize the window, the Heading stay in his position forever, he do not stay in the middle of the screen, he is staying at the place he was when the animation stoppped.

But if I don't touch his position :

 

TweenMax.from("h1", 1, { opacity: 0})

 

I can resize it all I want, it will stay in the middle.

 

My english is quite poor so i'm giving you a CodePen to visualize it.

 

How can I make h1 fade in from the right and then making it staying in the middle when we resize the screen ?

Thank's if someone is passing by ?

See the Pen dagxpW by Ziratsu (@Ziratsu) on CodePen

Link to comment
Share on other sites

It's a good idea to always set transform-related values via GSAP so that it fully understands your intent. Browsers report transforms in matrix() and matrix3d() which are inherently ambiguous in terms of rotation and scale, plus it doesn't factor in percentage-based values (they're baked into pixel-based ones). So it's impossible for GSAP to understand that you were trying to do translate(-50%, -50%) unless you set those via GSAP's xPercent and yPercent values. 

 

So you could just do this: 

//tell GSAP what the original values should be. 
TweenMax.set("h1", {xPercent:-50, yPercent:-50, x:0, y:0});

window.onload = function () {
   //now when GSAP animates the normal "x" value, it's distinct from xPercent. 
   TweenMax.from("h1", 1, { opacity: 0, x:200});
}

 

See the Pen 54fe438a0a2de0e7c7f319d6ac549d0a by GreenSock (@GreenSock) on CodePen

 

Is that what you're looking for? 

 

Happy tweening! 

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