Aleksei Posted February 27, 2021 Share Posted February 27, 2021 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 comment Share on other sites More sharing options...
tailbreezy Posted February 27, 2021 Share Posted February 27, 2021 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 comment Share on other sites More sharing options...
GreenSock Posted February 27, 2021 Share Posted February 27, 2021 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 1 Link to comment Share on other sites More sharing options...
Aleksei Posted February 27, 2021 Author Share Posted February 27, 2021 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 comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now