Jump to content
GreenSock

Search In
  • More options...
Find results that contain...
Find results in...
Aleksei

A strange animation jump with "box-sizing: border-box"

Recommended Posts

Hi!

I've noticed a strange behavior of element when animating its height from "0" to "auto".

In the Codepen attached you can see how the bottom padding of the <main> tag jumps at the end of unfolding animation.

I've found out that this problem happens because I use box-sizing: border-box in CSS. If I switch to content-box it solves the problem, but is there a way to fix it while keeping the border-box? Please, advice.

Thank you in advance.

 

See the Pen zYoWvMr by nordskill (@nordskill) on CodePen

Link to post
Share on other sites

Hello Aleksei,

 

This will probably solves your issue.

 

  if (closed) {
    
    gsap.to(main, {
      duration: 1,
      height: (_,t) => {
        let prevHeight = gsap.getProperty(t,'height')
        t.style.height = 'auto'
        let h = gsap.getProperty(t,'height')
        t.style.height = prevHeight + 'px'
        return h + 'px'
      },
      paddingBottom: 20
    });

 

Link to post
Share on other sites

Yeah, the problem is that you're changing the padding with your tween which affects the height. When you animate to a height of "auto", GSAP automatically does basically what @tailbreezy suggested, but it does so internally. In your case, though, when the tween starts and it does that calculation, the paddingBottom is 0, thus when it forces the "height" to "auto", it's calculated with padding as 0, thus it's 20px off in your case. 

 

You can add it in there using a function-based value: 

gsap.to(main, {
   duration: 1,
   height: (i, target) => {
     let cssText = target.style.cssText,
         newHeight;
     target.style.height = "auto";
     target.style.paddingBottom = "20px";
     newHeight = gsap.getProperty(target, "height");
     target.style.cssText = cssText;
     return newHeight;
   },
   paddingBottom: 20
 });

Here's a fork: 

See the Pen 305f57b1896b0b6f969c09e30bf54361?editors=0010 by GreenSock (@GreenSock) on CodePen

  • Like 1
Link to post
Share on other sites
39 minutes ago, GreenSock said:

the paddingBottom is 0, thus when it forces the "height" to "auto", it's calculated with padding as 0

Oh, right. I missed this fact. Thank you! I'll use your advice.

Link to post
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.

×