I'm trying to get the height of an element at run time. I have a navbar that becomes fixed when I scroll down. I want to set the padding of the container to equal the navbar's height (when fixed). The problem is that as soon as I set the position to fixed the offsetHeight becomes 0. I can get the height using the offsetHeight of the child elements and including margin/padding but I'm looking for a cleaner way.
This uses angular to add/remove classes.
<div class="nav-container" id="nvbr" (dblclick)="getHeight()">
<app-navbar [fixed]="_fixed"></app-navbar>
</div>
<div [style.padding-top.px]="_fixed ? _navHeight : 0">
<router-outlet></router-outlet>
</div>
Scroll event (this tells angular to add my fixed class to the element):
if (window.pageYOffset > 55) {
if (this._fixed === false)
this._fixed = true
}
else {
if (this._fixed === true)
this._fixed = false
}//else
Getting height:
getHeight() {
let navEl = document.getElementById("nvbr")
console.log(navEl, navEl.offsetHeight)
}
if I go into chrome and untick position:fixed; then I get the correct height
Any ideas?