Javascript 禁用浏览器窗口调整大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10610899/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 02:08:11  来源:igfitidea点击:

Disable Browser Window Resize

javascriptjquerybrowserwindow-resize

提问by Matt Bontrager

For starters... I have no sinister intention of subjecting users to popups or anything like that. I simply want to prevent a user from resizing the browser window of a webpage to which they've already navigated (meaning I don't have access to / don't want to use window.open();). I've been researching this for quite a while and can't seem to find a straightforward answer.

对于初学者......我没有让用户接受弹出窗口或类似内容的险恶意图。我只是想阻止用户调整他们已经导航到的网页的浏览器窗口的大小(这意味着我无权访问/不想使用 window.open();)。我一直在研究这个很长一段时间,似乎无法找到一个直接的答案。

I felt like I was on track with something along the lines of:

我觉得我正朝着以下方向走上正轨:

 $(window).resize(function() {
     var wWidth = window.width,
     wHeight = window.height;
     window.resizeBy(wWidth, wHeight);
 });

...to no avail. I have to imagine this is possible. Is it? If so, I would definitely appreciate the help.

……无济于事。我不得不想象这是可能的。是吗?如果是这样,我肯定会感谢您的帮助。

Thanks

谢谢

回答by u283863

You can first determine a definite size.

您可以先确定一个确定的大小。

var size = [window.width,window.height];  //public variable

Then do this:

然后这样做:

$(window).resize(function(){
    window.resizeTo(size[0],size[1]);
});

Demo: http://jsfiddle.net/DerekL/xeway917/

演示:http: //jsfiddle.net/DerekL/xeway917/



Q: Won't this cause an infinite loop of resizing? - user1147171

问:这不会导致无限循环调整大小吗?- 用户1147171

Nice question. This will not cause an infinite loop of resizing. The W3C specificationstates that resizeevent must be dispatched only when a document view has been resized. When the resizeTofunction try to execute the second time, the window will have the exact same dimension as it just set, and thus the browser will not fire the resize event because the dimensions have not been changed.

好问题。这不会导致无限循环调整大小。在W3C规范规定,resize只有当一个文档视图已调整大小事件必须被分派。当resizeTo函数尝试第二次执行时,窗口将具有与刚刚设置的完全相同的尺寸,因此浏览器不会触发 resize 事件,因为尺寸没有改变。

回答by Wesley Smith

I needed to do this today (for a panel opened by a chrome extension) but I needed to allow the user to change the window height, but prevent them changing the window width

我今天需要这样做(对于由 chrome 扩展程序打开的面板),但我需要允许用户更改窗口高度,但防止他们更改窗口宽度

@Derek's solution got me almost there but I had to tweak it to allow height changes and because of that, an endless resizing loop was possible so I needed to prevent that as well. This is my version of Dereck's answer that is working quite well for me:

@Derek 的解决方案让我几乎到了那里,但我不得不调整它以允许高度变化,因此,无限的调整大小循环是可能的,所以我也需要防止这种情况发生。这是我对 Dereck 的回答的版本,对我来说效果很好:

var couponWindow = {
  width: $(window).width(),
  height: $(window).height(),
  resizing: false
};
var $w=$(window);
$w.resize(function() {
  if ($w.width() != couponWindow.width && !couponWindow.resizing) {
    couponWindow.resizing = true;
    window.resizeTo(couponWindow.width, $w.height());
  }
  couponWindow.resizing = false;
});