Get page height in JS (Cross-Browser)

后端 未结 3 1523
温柔的废话
温柔的废话 2021-02-15 15:34

What is the best way to get the actual page (not window) height in JS that is cross-browser compatible?

I\'ve seen a few ways but they all return different values...

相关标签:
3条回答
  • 2021-02-15 16:17

    Try this without jQuery

    //Get height
    var myWidth = 0, myHeight = 0;
    if (typeof (window.innerWidth) == 'number') {
        myWidth = window.innerWidth; 
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        myWidth = document.documentElement.clientWidth; 
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
       myWidth = document.body.clientWidth; 
       myHeight = document.body.clientHeight;
    }
    

    Hope this helps you.

    0 讨论(0)
  • 2021-02-15 16:32
    var width = window.innerWidth ||
                html.clientWidth  ||
                body.clientWidth  ||
                screen.availWidth;
    
    var height = window.innerHeight ||
                 html.clientHeight  ||
                 body.clientHeight  ||
                 screen.availHeight;
    

    Should be a nice & clean way to accomplish it.

    0 讨论(0)
  • 2021-02-15 16:35

    Page/Document height is currently subject to vendor (IE/Moz/Apple/...) implementation and does not have a standard and consistent result cross-browser.

    Looking at JQuery .height() method;

    if ( jQuery.isWindow( elem ) ) {
                // Everyone else use document.documentElement or document.body depending on Quirks vs Standards mode
                // 3rd condition allows Nokia support, as it supports the docElem prop but not CSS1Compat
                var docElemProp = elem.document.documentElement[ "client" + name ],
                    body = elem.document.body;
                return elem.document.compatMode === "CSS1Compat" && docElemProp ||
                    body && body[ "client" + name ] || docElemProp;
    
            // Get document width or height
            } else if ( elem.nodeType === 9 ) {
                // Either scroll[Width/Height] or offset[Width/Height], whichever is greater
                return Math.max(
                    elem.documentElement["client" + name],
                    elem.body["scroll" + name], elem.documentElement["scroll" + name],
                    elem.body["offset" + name], elem.documentElement["offset" + name]
                );
    

    nodeType === 9 mean DOCUMENT_NODE : http://www.javascriptkit.com/domref/nodetype.shtml so no JQuery code solution should looks like:

    var height = Math.max(
                        elem.documentElement.clientHeight,
                        elem.body.scrollHeight, elem.documentElement.scrollHeight,
                        elem.body.offsetHeight, elem.documentElement.offsetHeight)
    
    0 讨论(0)
提交回复
热议问题