jQuery doesn't return proper body height on document load - Their bug or mine?

前端 未结 2 1470
失恋的感觉
失恋的感觉 2021-01-11 10:22

When I use

$(document).ready(function() {
 var bodyHeight = $(\"body\").height();
 console.log(bodyHeight);
});

I get a really wack number

2条回答
  •  长情又很酷
    2021-01-11 11:19

    The document.ready event fires after the DOM has loaded but before the page has fully rendered. If you want to get the correct height, you should try the window.load event which fires after all images and objects have been loaded and the page has been rendered:

    $(window).load(function() {
        var bodyHeight = $("body").height();
    });
    

提交回复
热议问题