如您所知,在 Internet Explorer 中,当页面上的任何元素调整大小时,将触发 window.resize 事件。页面元素是否通过分配/更改其高度或样式属性、通过简单地向其添加子元素或其他方式来调整页面元素的大小并不重要——即使元素调整大小不会影响视口本身的尺寸。
在我的应用程序中,这会导致令人讨厌的递归,因为在我的 window.resize 处理程序中,我正在调整一些
元素的大小,这反过来会重新触发 window.resize 等。同样,这只是 IE 中的问题。
有什么办法可以阻止 window.resize 在 IE 中触发以响应页面上正在调整大小的元素?
我还应该提到我正在使用 jQuery。
我刚刚发现了另一个可能对你有帮助的问题。
我正在使用 jQuery,并且有 window.resize 事件来调用一个函数,该函数将重新定位附加到正文的 div。
现在,当我设置附加 div 的 LEFT css 属性时,window.resize 事件会无缘无故地被触发。
它会导致无限循环,一次又一次触发 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;
});
所以基本上它会检查高度或宽度是否改变(只有当你实际调整窗口大小时才会发生)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)