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

前端 未结 2 1450
失恋的感觉
失恋的感觉 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:02

    You want to look at the $(window).load() function rather than $(document).ready().

    The $(document).ready() event executes when the HTML DOM is loaded and ready, even if all the graphics haven't loaded yet. The $(window).load() event executes later when the complete page is fully loaded, including all frames, objects and images.

    Here is a great link describing the difference. http://4loc.wordpress.com/2009/04/28/documentready-vs-windowload/

    0 讨论(0)
  • 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();
    });
    
    0 讨论(0)
提交回复
热议问题