简体   繁体   English

强制调整窗口大小以触发

[英]Forcing windows resize to fire

I have problems with my layout, when I re-size window manually and restore it to normal everything is in the perfect place.我的布局有问题,当我手动调整窗口大小并将其恢复正常时,一切都在完美的地方。

Is it possible to trick the browser and do something with JavaScript that would actually make the browser think that the page has been re-sized so that my layout looks as it should?是否有可能欺骗浏览器并使用 JavaScript 做一些实际上会使浏览器认为页面已重新调整大小以便我的布局看起来应该如此的事情?

UPDATE:更新:

Can I re-size and restore window so that user barely notices?我可以重新调整窗口大小和恢复窗口以便用户几乎不会注意到吗?

从那时起,一个更好的解决方案已发布在https://stackoverflow.com/a/18693617/2306587 中

window.dispatchEvent(new Event('resize'));

You can resize the window like this...您可以像这样调整窗口大小...

window.resizeTo(width, height);

And if you need to trigger the event handler, you can do that like this...如果你需要触发事件处理程序,你可以这样做......

window.onresize();

您可以使用 jQuery resize()方法,如下所示:

$(window).resize();

This is possible with Mootools (which means it can be done without a framework as well), here's an example:这可以通过Mootools 实现(这意味着它也可以在没有框架的情况下完成),这是一个示例:

window.addEvent('domready', function() {
 window.addEvent('resize', function() {
  alert('f');
 });
 (function() {
  window.fireEvent('resize');
 }).delay(3000);
});

3 seconds after the DOM-structure has loaded, the resize-event will be fired for the window-object. DOM 结构加载 3 秒后,将为窗口对象触发调整大小事件。

Edit;编辑;

I have no clue how Mootools solves their fireEvent for the resize event.我不知道 Mootools 如何为 resize 事件解决他们的 fireEvent。 I tried Google, but found nada.我试过谷歌,但找到了 nada。 You can of course resize the window manually, but this is really a hack:您当然可以手动调整窗口大小,但这确实是一个技巧:

function fireOnResizeEvent() {
 var width, height;

 if (navigator.appName.indexOf("Microsoft") != -1) {
  width  = document.body.offsetWidth;
  height = document.body.offsetHeight;
 } else {
  width  = window.outerWidth;
  height = window.outerHeight;
 }

 window.resizeTo(width - 1, height);
 window.resizeTo(width + 1, height);
}

window.onresize = function() {
 alert("Resized!");
}

Following is the working solution:以下是工作解决方案:

if (typeof(Event) === 'function') {
  // modern browsers
  window.dispatchEvent(new Event('resize'));
} else {
  // for IE and other old browsers
  // causes deprecation warning on modern browsers
  var evt = window.document.createEvent('UIEvents'); 
  evt.initUIEvent('resize', true, false, window, 0); 
  window.dispatchEvent(evt);
}

toggle the display attribute on the areas that need to be redrawn.在需要重绘的区域上切换显示属性。 for example:例如:

var toDraw = document.getElementById('redrawme');
toDraw.style.display = none;
toDraw.style.display = '';

I dont know whqt the result would be if you tried to do the entire body element as ive never tired it before.我不知道如果你尝试做整个身体元素,结果会是什么,因为我以前从不厌倦它。

What are you doing that you need to force a redraw?你在做什么需要强制重绘?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM