简体   繁体   中英

Calculate height of a dynamically loaded div

I have a requirement where my header height changes.The header takes some time or say delay while loading.And based on the header height,my body's height has to be calculated and should be set accordingly.

Here's the sample code:

<div class="header header.row">
header content
</div>

<div class="main main.row">
body content
</div>

JS Code:

document.ready(function()
{
main.top=header.height;
});

My concern is how can we know when the header div is completely loaded.Is there any way to find that the header is finally loaded and then calculate the body position.Workaround tried is setting up delay of 750ms.But as delay normally won't work in real scenarios,trying to find alternatives.

Any help is surely appreciated.

Thanks in advance.

With jQuery it's something like this:

$(window).load(function() {
    $('.main').css('top', $('.header').height());
});

You can listen for the onload event, either on window or document eg

document.onload = function() {
  var height = document.getElementsByClassName('header')[0].clientHeight;
}

Note, don't need header and header.row ; you should just use header row to give the div both classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM