Detect if browser window is maximized and at default (100%) zoom

前端 未结 1 1792
情深已故
情深已故 2021-02-11 11:33

I know it sounds weird but there really is a reason and it is in the users\' best interest. I know that having the browser automatically maximize and set 100% might be problemat

相关标签:
1条回答
  • 2021-02-11 12:15

    Detect if at max width:

    You'll probably want to check if the document's width is lesser than the screen width:

    var isAtMaxWidth = screen.availWidth - window.innerWidth === 0;
    

    You can't do this with height however, as the browser has reserved vertical space for tabs and toolbars, and there is no way of getting the height of those.

    Detect browser zoom:

    Based on this answer by user800583, here's a shortened version:

    var screenPixelRatio = (window.outerWidth - 8) / window.innerWidth;
    var isAtDefaultZoom = screenPixelRatio > 0.92 && screenPixelRatio <= 1.10;
    

    N.B.: You can't use window.devicePixelRatio to detect this, as high-DPI (e.g.: retina) displays will have different base values.

    Combined check:

    var isMaximizedAndDefaultZoom = isAtMaxWidth && isAtDefaultZoom;
    

    Tested and working on Chrome 64 as of 06 Mar 2018

    0 讨论(0)
提交回复
热议问题