我剛剛發現了另一個問題,這可能會幫助你。
我使用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