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

霸气de小男生 提交于 2019-12-03 22:00:56

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!