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

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

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

19条回答
  •  误落风尘
    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.

提交回复
热议问题