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

你说的曾经没有我的故事 提交于 2019-12-05 08:17:01

问题


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 problematic but how about making so that if the window is not maximized and the zoom is not 100% all the users would see is a message "please max your window and set the zoom to 100%". I need to make this work in Chrome, Firefox and IE ...at least.

I am not trying to make it "full screen mode" just maximize the window.

If "forcing" in the sense "keeping" 100% and max window is problematic how about just setting the zoom to 100% and maximizing the window on initial load?


回答1:


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



来源:https://stackoverflow.com/questions/49003290/detect-if-browser-window-is-maximized-and-at-default-100-zoom

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