I'm trying to replicate jQuery slideDown()
in GSAP and I'm having trouble working out how jQuery calculates the height of an item which is currently hidden as if it was set to height:auto
.
I've tried trawling the code on GitHub but can't find any code which seems to be doing this in jQuery.fn.slideDown
or jQuery.fn.animate
which it calls.
There are several similar questions on SO and several solutions proposed, all of which seem to have their own problems:
Clone the element, position it off screen and calculate its height.
This won't work if the element or any of its child elements have a height set by CSS styles which require the element to be in its original place in the DOM (e.g. an.accordianItem
might only be styled if it's inside its.accordian
).Display the item, remove
height:0
and quickly calculate the height before hiding the element again and then stating the animation.
This might flash the content quickly while calculating the height.Use
visibility:true
to show it in place while calculating the height.
This would stop the flash and still keep the element in the same position in the DOM for correct height calculation, but it would still push other items below it down becausevisibility:false
items still have a height.Calculate the height of an item before it's hidden and store it in a data attribute so we know it when we want to open the item later.
This won't work if any dynamic content changes the height of the item whilst it's hidden.
jQuery slideDown()
"just works" every time so I'd be really interested to know how it works, but I just can't work out where it's doing this. I'm also surprised that GSAP can't do this out of the box, or that nobody has shared a proper solution to this before.
Any help would really be appreciated.