How to get height of entire document with JavaScript?

前端 未结 13 1781
逝去的感伤
逝去的感伤 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!

提交回复
热议问题