我刚刚发现了另一个问题,这可能会帮助你。
我使用jQuery和我有window.resize事件来调用一个函数,将重新定位附加到正文的div。
现在,当我设置该附加div的LEFT css属性时,window.resize事件获取触发器为NO GOOD REASON。
它导致无限循环,一次又一次触发window.resize。
无需修改代码:
$(window).resize(function(){
var onResize = function(){
//The method which alter some css properties triggers
//window.resize again and it ends in an infinite loop
someMethod();
}
window.clearTimeout(resizeTimeout);
resizeTimeout = window.setTimeout(onResize, 10);
});
解决方法:
var winWidth = $(window).width(),
winHeight = $(window).height();
$(window).resize(function(){
var onResize = function(){
//The method which alter some css properties triggers
//window.resize again and it ends in an infinite loop
someMethod();
}
//New height and width
var winNewWidth = $(window).width(),
winNewHeight = $(window).height();
// compare the new height and width with old one
if(winWidth!=winNewWidth || winHeight!=winNewHeight){
window.clearTimeout(resizeTimeout);
resizeTimeout = window.setTimeout(onResize, 10);
}
//Update the width and height
winWidth = winNewWidth;
winHeight = winNewHeight;
});
所以基本上如果高度或宽度改变,它会检查(当你真正用窗口调整这只会发生)。
有趣的,我不知道。我不熟悉JQuery,但如果它的源元素不是窗口本身,应该可以停止事件。我确信有人会想出一个编码解决方案。 – 2009-12-05 17:09:08
这是什么版本的IE浏览器,你可以提供一些代码,实际显示调整大小的元素激发window.resize? – DEfusion 2009-12-08 13:30:29
不应该关于其他版本,但它发生在IE 8 – Cocowalla 2013-01-22 18:48:28