Jump to content
GreenSock

MikeWillis

Animation runs choppy - how can I fix it?

Recommended Posts

Can anyone give me some pointers for making the show/hide animation smooth? I wanted something that could replace jQuery's show/hide with a "blind" effect. The element I'm working with is centered using margin: 0px auto; and I can't be sure of the final height. So basically I'd like to animate from visible down to height/margin/padding 0, and invisible up to height auto (margin/padding being whatever they might have been previously).

 

To do so I made a .GSAPHideMe_CenteredElement class that I toggle, which has properties for height / margin etc. The "hide" version of the tween should animate to those settings, then assign the class to the object and clear the settings. The "show" version pretty much just removes the class.

 

This all works, but it's choppy.. Is there a better way to go about it?

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

Link to comment
Share on other sites

Hi @MikeWillis,

 

My suggestion would be to break the animation down into parts to find out what part of the code is causing it to be choppy. I wasn't able to easily read enough of what's happening in your code, to give a direct suggestion. I think you may be better of using a play() / reverse() on toggle the show hide too, for simplicity sake (see this nav toggle I built a while back

See the Pen Vezxxy?editors=1010 by ryan_labar (@ryan_labar) on CodePen

).

 

 

 

  • Like 4
Link to comment
Share on other sites

On 2/1/2019 at 3:37 PM, MikeWillis said:

This all works, but it's choppy.. Is there a better way to go about it?

 

Sure. Don't animate all the things...

 

IliP14a.jpg

 

 

#banner-message {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  font-size: 25px;
  text-align: center;
/*   transition: all 0.2s; */
  margin: 0 auto;
  width: 75%;
}

 

CSS and JS can't animate the same properties. If you need CSS transitions on an element you're animating with JS, you should explicitly list those properties. Using "all" can also impact performance because the browser has to watch for changes to all those properties. 

 

Also, there's no such thing as an int or float type in JavaScript. Only "number".

 

if ( arguments.length >= 5 && (typeof(speed) == "float" || typeof(speed) == "int") ) {
  Settings.speed = speed;
}

 

 

  • Like 6
  • Haha 1
Link to comment
Share on other sites

On 2/1/2019 at 8:12 PM, OSUblake said:

 


#banner-message {
  background: #fff;
  border-radius: 4px;
  padding: 20px;
  font-size: 25px;
  text-align: center;
/*   transition: all 0.2s; */
  margin: 0 auto;
  width: 75%;
}

 

 

PERFECT - animation is completely smooth now! What a difference!

 

On 2/1/2019 at 8:12 PM, OSUblake said:

Also, there's no such thing as an int or float type in JavaScript. Only "number".

 

 

Duh - good point - too much PHP that day!

 

thanks OSUblake!

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