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

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

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

19条回答
  •  北荒
    北荒 (楼主)
    2020-11-21 06:33

    If you need a truly bulletproof solution for the document width and height (the pageWidth and pageHeight in the picture), you might want to consider using a plugin of mine, jQuery.documentSize.

    It has just one purpose: to always return the correct document size, even in scenarios when jQuery and other methods fail. Despite its name, you don't necessarily have to use jQuery – it is written in vanilla Javascript and works without jQuery, too.

    Usage:

    var w = $.documentWidth(),
        h = $.documentHeight();
    

    for the global document. For other documents, e.g. in an embedded iframe you have access to, pass the document as a parameter:

    var w = $.documentWidth( myIframe.contentDocument ),
        h = $.documentHeight( myIframe.contentDocument );
    

    Update: now for window dimensions, too

    Ever since version 1.1.0, jQuery.documentSize also handles window dimensions.

    That is necessary because

    • $( window ).height() is buggy in iOS, to the point of being useless
    • $( window ).width() and $( window ).height() are unreliable on mobile because they don't handle the effects of mobile zooming.

    jQuery.documentSize provides $.windowWidth() and $.windowHeight(), which solve these issues. For more, please check out the documentation.

提交回复
热议问题