Browser size (width and height)

后端 未结 5 2240
我寻月下人不归
我寻月下人不归 2020-12-08 04:14

I\'m trying to detect the browser\'s current size (width and height). I know it\'s super easy in jQuery with $(document).width and $(document).height, but I don

5条回答
  •  再見小時候
    2020-12-08 04:50

    function getWindowSize(){
     var d= document, root= d.documentElement, body= d.body;
     var wid= window.innerWidth || root.clientWidth || body.clientWidth, 
     hi= window.innerHeight || root.clientHeight || body.clientHeight ;
     return [wid,hi]
    }
    

    IE browsers are the only ones who don't use innerHeight and Width.

    But there is no 'standard'- just browser implementations.

    Test the html (document.documentElement) clientHeight before checking the body- if it is not 0, it is the height of the 'viewport' and the body.clientHeight is the height of the body- which can be larger or smaller than the window.

    Backwards mode returns 0 for the root element and the window (viewport) height from the body.

    Same with width.

提交回复
热议问题