Get the size of the screen, current web page and browser window

前端 未结 19 2413
面向向阳花
面向向阳花 2020-11-21 05:29

How can I get windowWidth, windowHeight, pageWidth, pageHeight, screenWidth, screenHeight,

19条回答
  •  不思量自难忘°
    2020-11-21 06:16

    Complete guide related to Screen sizes

    JavaScript

    For height:

    document.body.clientHeight  // Inner height of the HTML document body, including padding 
                                // but not the horizontal scrollbar height, border, or margin
    
    screen.height               // Device screen height (i.e. all physically visible stuff)
    screen.availHeight          // Device screen height minus the operating system taskbar (if present)
    window.innerHeight          // The current document's viewport height, minus taskbars, etc.
    window.outerHeight          // Height the current window visibly takes up on screen 
                                // (including taskbars, menus, etc.)
    

    Note: When the window is maximized this will equal screen.availHeight

    For width:

    document.body.clientWidth   // Full width of the HTML page as coded, minus the vertical scroll bar
    screen.width                // Device screen width (i.e. all physically visible stuff)
    screen.availWidth           // Device screen width, minus the operating system taskbar (if present)
    window.innerWidth           // The browser viewport width (including vertical scroll bar, includes padding but not border or margin)
    window.outerWidth           // The outer window width (including vertical scroll bar,
                                // toolbars, etc., includes padding and border but not margin)
    

    Jquery

    For height:

    $(document).height()    // Full height of the HTML page, including content you have to 
                            // scroll to see
    
    $(window).height()      // The current document's viewport height, minus taskbars, etc.
    $(window).innerHeight() // The current document's viewport height, minus taskbars, etc.
    $(window).outerHeight() // The current document's viewport height, minus taskbars, etc.                         
    

    For width:

    $(document).width()     // The browser viewport width, minus the vertical scroll bar
    $(window).width()       // The browser viewport width (minus the vertical scroll bar)
    $(window).innerWidth()  // The browser viewport width (minus the vertical scroll bar)
    $(window).outerWidth()  // The browser viewport width (minus the vertical scroll bar)
    

    Reference: https://help.optimizely.com/Build_Campaigns_and_Experiments/Use_screen_measurements_to_design_for_responsive_breakpoints

提交回复
热议问题