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

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

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

相关标签:
19条回答
  • 2020-11-21 06:22

    here is my solution!

    // innerWidth
    const screen_viewport_inner = () => {
        let w = window,
            i = `inner`;
        if (!(`innerWidth` in window)) {
            i = `client`;
            w = document.documentElement || document.body;
        }
        return {
            width: w[`${i}Width`],
            height: w[`${i}Height`]
        }
    };
    
    
    // outerWidth
    const screen_viewport_outer = () => {
        let w = window,
            o = `outer`;
        if (!(`outerWidth` in window)) {
            o = `client`;
            w = document.documentElement || document.body;
        }
        return {
            width: w[`${o}Width`],
            height: w[`${o}Height`]
        }
    };
    
    // style
    const console_color = `
        color: rgba(0,255,0,0.7);
        font-size: 1.5rem;
        border: 1px solid red;
    `;
    
    
    
    // testing
    const test = () => {
        let i_obj = screen_viewport_inner();
        console.log(`%c screen_viewport_inner = \n`, console_color, JSON.stringify(i_obj, null, 4));
        let o_obj = screen_viewport_outer();
        console.log(`%c screen_viewport_outer = \n`, console_color, JSON.stringify(o_obj, null, 4));
    };
    
    // IIFE
    (() => {
        test();
    })();

    0 讨论(0)
  • 2020-11-21 06:23

    You can use the Screen object to get this.

    The following is an example of what it would return:

    Screen {
        availWidth: 1920,
        availHeight: 1040,
        width: 1920,
        height: 1080,
        colorDepth: 24,
        pixelDepth: 24,
        top: 414,
        left: 1920,
        availTop: 414,
        availLeft: 1920
    }
    

    To get your screenWidth variable, just use screen.width, same with screenHeight, you would just use screen.height.

    To get your window width and height, it would be screen.availWidth or screen.availHeight respectively.

    For the pageX and pageY variables, use window.screenX or Y. Note that this is from the VERY LEFT/TOP OF YOUR LEFT/TOP-est SCREEN. So if you have two screens of width 1920 then a window 500px from the left of the right screen would have an X value of 2420 (1920+500). screen.width/height, however, display the CURRENT screen's width or height.

    To get the width and height of your page, use jQuery's $(window).height() or $(window).width().

    Again using jQuery, use $("html").offset().top and $("html").offset().left for your pageX and pageY values.

    0 讨论(0)
  • 2020-11-21 06:24

    A non-jQuery way to get the available screen dimension. window.screen.width/height has already been put up, but for responsive webdesign and completeness sake I think its worth to mention those attributes:

    alert(window.screen.availWidth);
    alert(window.screen.availHeight);
    

    http://www.quirksmode.org/dom/w3c_cssom.html#t10 :

    availWidth and availHeight - The available width and height on the screen (excluding OS taskbars and such).

    0 讨论(0)
  • 2020-11-21 06:25

    Here is a cross browser solution with pure JavaScript (Source):

    var width = window.innerWidth
    || document.documentElement.clientWidth
    || document.body.clientWidth;
    
    var height = window.innerHeight
    || document.documentElement.clientHeight
    || document.body.clientHeight;
    
    0 讨论(0)
  • 2020-11-21 06:26

    You can get the size of the window or document with jQuery:

    // Size of browser viewport.
    $(window).height();
    $(window).width();
    
    // Size of HTML document (same as pageHeight/pageWidth in screenshot).
    $(document).height();
    $(document).width();
    

    For screen size you can use the screen object:

    window.screen.height;
    window.screen.width;
    
    0 讨论(0)
  • 2020-11-21 06:26

    To check height and width of your current loaded page of any website using "console" or after clicking "Inspect".

    step 1: Click the right button of mouse and click on 'Inspect' and then click 'console'

    step 2: Make sure that your browser screen should be not in 'maximize' mode. If the browser screen is in 'maximize' mode, you need to first click the maximize button (present either at right or left top corner) and un-maximize it.

    step 3: Now, write the following after the greater than sign ('>') i.e.

           > window.innerWidth
                output : your present window width in px (say 749)
    
           > window.innerHeight
                output : your present window height in px (say 359)
    
    0 讨论(0)
提交回复
热议问题