How to get document height and width without using jquery

后端 未结 10 1347
有刺的猬
有刺的猬 2020-11-28 05:26

How to get document height and width in pure javascript i.e without using jquery.
I know about $(document).height() and $(document).width(), bu

相关标签:
10条回答
  • 2020-11-28 06:03

    You should use getBoundingClientRect as it usually works cross browser and gives you sub-pixel precision on the bounds rectangle.

    elem.getBoundingClientRect()
    
    0 讨论(0)
  • 2020-11-28 06:08

    You can try also:

    document.body.offsetHeight
    document.body.offsetWidth
    
    0 讨论(0)
  • 2020-11-28 06:09

    window is the whole browser's application window. document is the webpage shown that is actually loaded.

    window.innerWidth and window.innerHeight will take scrollbars into account which may not be what you want.

    document.documentElement is the full webpage without the top scrollbar. document.documentElement.clientWidth returns document width size without y scrollbar. document.documentElement.clientHeight returns document height size without x scrollbar.

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

    This should work for all browsers/devices:

    function getActualWidth()
    {
        var actualWidth = window.innerWidth ||
                          document.documentElement.clientWidth ||
                          document.body.clientWidth ||
                          document.body.offsetWidth;
    
        return actualWidth;
    }
    
    0 讨论(0)
提交回复
热议问题