Jump to content
Search Community

TimelineLite: from height:0 to height:auto doesn't work properly

anonty 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

I'm having problems with TimelineLite and animating height:0 to height:auto. When I click the button multiple times, sometimes it sets height to 'auto' instead of reversing the timeline to height:0. So for example I get this result:

 

Expand = true: height = auto

Expand = false: height = 0

Expand = true: height = auto

Expand = false: height = auto << wrong, should be 0

Expand = true: height = auto

Expand = false: height = 0

 

What's wrong with my code?

See the Pen gGwEPv by anon (@anon) on CodePen

Link to comment
Share on other sites

That's actually a very tricky scenario just because of the way you coded it.

 

First, the solution
 

//OLD: 
tl = new TimelineLite();
tl.set($('#menu'), { css: { display: 'block', height: 'auto' } })
  .from($('#menu'), 0.6, { height: 0, ease: Power3.easeInOut }, 0);

//NEW: 
TweenLite.set("#menu", {display:"block", height:"auto"});
tl = TweenLite.from('#menu', 0.6, { height: 0, ease: Power3.easeInOut }, 0); 

 

Notice that I'm not embedding the initial set() call in the timeline itself. 

 

The explanation:

You had a set() call embedded at the VERY start of the timeline which sets height to "auto", so imagine what's supposed to happen when the playhead goes forward and then comes back (after reversing the timeline). When the playhead hits the beginning...if it's EXACTLY on top of that time (0), it'll actually tell it to render as height:"auto". After all, those instructions are there on the timeline. The reason you only intermittently saw that was because the playhead would literally have to land exactly on that spot, but it's more typical for it to be just after or before it ("ticks" are usually 16.6ms apart, but depend on many factors). If the playhead lands BEFORE that spot (like literally a negative time...which isn't allowable, but GSAP still senses that condition in order to properly set state), in this case it'll tell that set() to revert to its start state, which means height probably wasn't "auto" (it's whatever it was at the time that tween rendered the first time). 

 

I know, it probably sounds a little complex. It kinda is. 

 

So a much cleaner and more performant way to handle this is to just do a regular TweenLite (or TweenMax) .set() call that just fires off that one time initially, and doesn't get embedded in the timeline.

 

Hopefully that clears things up. 

  • Like 3
  • Thanks 1
Link to comment
Share on other sites

Nope, in this case there's no advantage to using TweenLite.set() over JS .style or jQuery.css(). 

 

The only reasons TweenLite.set() can be advantageous are:

  1. It uses the same API as GSAP, thus you can set properties like x, y, rotation, drawSVG, and other shortcuts/special properties. Again, not necessary here because you're doing something standard ("height")
  2. TweenLite.set() is actually a zero-duration tween, thus it records starting values and ending values. If you put it into a timeline, when the playhead moves past it forward, it'll set the values accordingly, and if the playhead moves backward (in front of where that set() is located), it'll revert the values for you. This can be very useful in certain cases. 
  3. Technically, since it is a zero-duration tween, you could set a delay on it and then restart(true) it to have it act kinda like a timer that'd set the value after that delay elapses. And of course that'd be perfectly synchronized with the whole GSAP engine. 

Since none of those matter in your case, you're fine using plain JS or jQuery.css(). 

 

Enjoy!

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