How to get height of entire document with JavaScript?

前端 未结 13 1776
逝去的感伤
逝去的感伤 2020-11-22 06:01

Some documents I can\'t get the height of the document (to position something absolutely at the very bottom). Additionally, a padding-bottom on seems to do nothing on these

相关标签:
13条回答
  • 2020-11-22 06:26

    Full Document height calculation:

    To be more generic and find the height of any document you could just find the highest DOM Node on current page with a simple recursion:

    ;(function() {
        var pageHeight = 0;
    
        function findHighestNode(nodesList) {
            for (var i = nodesList.length - 1; i >= 0; i--) {
                if (nodesList[i].scrollHeight && nodesList[i].clientHeight) {
                    var elHeight = Math.max(nodesList[i].scrollHeight, nodesList[i].clientHeight);
                    pageHeight = Math.max(elHeight, pageHeight);
                }
                if (nodesList[i].childNodes.length) findHighestNode(nodesList[i].childNodes);
            }
        }
    
        findHighestNode(document.documentElement.childNodes);
    
        // The entire page height is found
        console.log('Page height is', pageHeight);
    })();
    

    You can Test it on your sample sites (http://fandango.com/ or http://paperbackswap.com/) with pasting this script to a DevTools Console.

    NOTE: it is working with Iframes.

    Enjoy!

    0 讨论(0)
  • 2020-11-22 06:26

    For anyone having trouble scrolling a page on demand, using feature detection, I've come up with this hack to detect which feature to use in an animated scroll.

    The issue was both document.body.scrollTop and document.documentElement always returned true in all browsers.

    However you can only actually scroll a document with either one or the other. d.body.scrollTop for Safari and document.documentElement for all others according to w3schools (see examples)

    element.scrollTop works in all browsers, not so for document level.

        // get and test orig scroll pos in Saf and similar 
        var ORG = d.body.scrollTop; 
    
        // increment by 1 pixel
        d.body.scrollTop += 1;
    
        // get new scroll pos in Saf and similar 
        var NEW = d.body.scrollTop;
    
        if(ORG == NEW){
            // no change, try scroll up instead
            ORG = d.body.scrollTop;
            d.body.scrollTop -= 1;
            NEW = d.body.scrollTop;
    
            if(ORG == NEW){
                // still no change, use document.documentElement
                cont = dd;
            } else {
                // we measured movement, use document.body
                cont = d.body;
            }
        } else {
            // we measured movement, use document.body
            cont = d.body;
        }
    
    0 讨论(0)
  • 2020-11-22 06:28

    Document sizes are a browser compatibility nightmare because, although all browsers expose clientHeight and scrollHeight properties, they don't all agree how the values are calculated.

    There used to be a complex best-practice formula around for how you tested for correct height/width. This involved using document.documentElement properties if available or falling back on document properties and so on.

    The simplest way to get correct height is to get all height values found on document, or documentElement, and use the highest one. This is basically what jQuery does:

    var body = document.body,
        html = document.documentElement;
    
    var height = Math.max( body.scrollHeight, body.offsetHeight, 
                           html.clientHeight, html.scrollHeight, html.offsetHeight );
    

    A quick test with Firebug + jQuery bookmarklet returns the correct height for both cited pages, and so does the code example.

    Note that testing the height of the document before the document is ready will always result in a 0. Also, if you load more stuff in, or the user resizes the window, you may need to re-test. Use onload or a document ready event if you need this at load time, otherwise just test whenever you need the number.

    0 讨论(0)
  • 2020-11-22 06:28

    This cross browser code below evaluates all possible heights of the body and html elements and returns the max found:

                var body = document.body;
                var html = document.documentElement;
                var bodyH = Math.max(body.scrollHeight, body.offsetHeight, body.getBoundingClientRect().height, html.clientHeight, html.scrollHeight, html.offsetHeight); // The max height of the body
    

    A working example:

    function getHeight()
    {
      var body = document.body;
      var html = document.documentElement; 
      var bodyH = Math.max(body.scrollHeight, body.offsetHeight, body.getBoundingClientRect().height, html.clientHeight, html.scrollHeight, html.offsetHeight);
      return bodyH;
    }
    
    document.getElementById('height').innerText = getHeight();
    body,html
    {
      height: 3000px;
    }
    
    #posbtm
    {
      bottom: 0;
      position: fixed;
      background-color: Yellow;
    }
    <div id="posbtm">The max Height of this document is: <span id="height"></span> px</div>
    
    example document body content example document body content example document body content example document body content <br />
    example document body content example document body content example document body content example document body content <br />
    example document body content example document body content example document body content example document body content <br />
    example document body content example document body content example document body content example document body content <br />
    example document body content example document body content example document body content example document body content <br />
    example document body content example document body content example document body content example document body content <br />
    example document body content example document body content example document body content example document body content <br />
    example document body content example document body content example document body content example document body content <br />
    example document body content example document body content example document body content example document body content <br />
    example document body content example document body content example document body content example document body content <br />
    example document body content example document body content example document body content example document body content <br />
    example document body content example document body content example document body content example document body content <br />
    example document body content example document body content example document body content example document body content <br />
    example document body content example document body content example document body content example document body content <br />
    example document body content example document body content example document body content example document body content <br />
    example document body content example document body content example document body content example document body content <br />
    example document body content example document body content example document body content example document body content <br />
    example document body content example document body content example document body content example document body content <br />
    example document body content example document body content example document body content example document body content <br />
    example document body content example document body content example document body content example document body content <br />
    example document body content example document body content example document body content example document body content <br />
    example document body content example document body content example document body content example document body content <br />
    example document body content example document body content example document body content example document body content <br />
    example document body content example document body content example document body content example document body content <br />

    0 讨论(0)
  • 2020-11-22 06:30

    You can even use this:

    var B = document.body,
        H = document.documentElement,
        height
    
    if (typeof document.height !== 'undefined') {
        height = document.height // For webkit browsers
    } else {
        height = Math.max( B.scrollHeight, B.offsetHeight,H.clientHeight, H.scrollHeight, H.offsetHeight );
    }
    

    or in a more jQuery way (since as you said jQuery doesn't lie) :)

    Math.max($(document).height(), $(window).height())
    
    0 讨论(0)
  • 2020-11-22 06:30

    Add References properly

    In my case I was using a ASCX page and the aspx page that contains the ascx control is not using the references properly. I just pasted the following code and it worked :

    <script src="../js/jquery-1.3.2-vsdoc.js" type="text/javascript"></script>
    <script src="../js/jquery-1.3.2.js" type="text/javascript"></script>
    <script src="../js/jquery-1.5.1.js" type="text/javascript"></script>
    
    0 讨论(0)
提交回复
热议问题