简体   繁体   English

为什么我的JavaScript onresize会触发两次?

[英]Why does my JavaScript onresize fire twice?

I have a fiddle here: 我在这里摆弄:

http://jsfiddle.net/fJMe9/ http://jsfiddle.net/fJMe9/

window.onresize = function (e) {
    console.log("Page resized");
};

And every time I resize the window I get two logs to the console 每次我调整窗口大小时,都会向控制台发送两个日志

It's a well known bug (perhaps relating to event bubbling? I say well known, but that's other people who know it, not me :P ). 这是一个众所周知的错误(也许与事件冒泡有关?我说是众所周知的,但是其他人知道它,而不是我:P)。 Use a setTimeout to check the last time the window was resized to avoid this. 使用setTimeout检查上一次调整窗口大小的时间,以避免这种情况。

Try: 尝试:

window.onresize = function (e) {
    console.log(e);
};

you'll see the event fires every time you drag the browser window 您会在每次拖动浏览器窗口时看到触发事件

Depends on implementation: maybe 2 times, the first for tell you the window is being resized and the second for windows finished resizing. 取决于实现方式:可能是2次,第一次用于告诉您调整窗口大小,第二次用于完成调整窗口大小。

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

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