I am trying to create a script, that opens and closes the menu. the problem is that I need to figure out the element`s height while it-s height is set to 0 from css.
I come out with a really ugly solution, I wonder if there is a more elegant way to do this.
my solution is to set visibility to hidden, then set height to auto, then query the height, after that to set back the height, and visibility.
I need this for css animation, because css anim it is not working with height auto, and height 0px, I need to have the height in px.
this is my code:
document.getElementById('menu_button').onclick = function() {
if (document.getElementById('mobile-menu').style.height == '0px') {
document.getElementById('mobile-menu').style.visibility = 'hidden';
document.getElementById('mobile-menu').style.height = 'auto';
var MobileMenuHeight = document.getElementById('mobile-menu').clientHeight;
document.getElementById('mobile-menu').style.height = '0px';
document.getElementById('mobile-menu').style.visibility = 'visible';
setTimeout(() => { document.getElementById('mobile-menu').style.height = MobileMenuHeight + 'px'; }, 50);
} else {
document.getElementById('mobile-menu').style.height = "0px";
}
}