Get page height in JS (Cross-Browser)

后端 未结 3 1521
温柔的废话
温柔的废话 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.

提交回复
热议问题