今天公司要搬家,实在是吵吵闹闹搞了一天,学了一点javascript
ie 6 , ie 7 在window resize 的时候 会出现多次(简单的说 在这些浏览器下面 resize是一个动态的过程,从开始位置到最终大小,他们不止一次的触发resize事件)
$(function(){ $(window).resize(function(){ //正常情况 alert(1); //IE 6 , IE7 个人想出的解决方法 height = $(window).height(); eventId = setTimeout(function(){ newHeight = $(window).height(); if(newHeight == height){ alert(1); clearTimeout(eventId); } },1000) }); }) //上面的方法有问题现在需要修复一下 //date 2014 12 20 tempHeight = 0 ; tempWidth = 0 ; timeoutId = 0 ; var clearId = function(max){ for(var i = 0 ; i <= max ; i++){ clearTimeout(i); } } $(window).resize(function(){ timeoutId = setTimeout(function(){ alert(1); alert("time out id is "+timeoutId); clearId(timeoutId); },500); })
上面的方法是我自己用来解决的,下面提供一个网上流传更为广阔的方法,效率可能更高一点
$(window).resize($.debounce(1000, function(){ alert(1); }));
http://stackoverflow.com/questions/1500312/dom-onresize-event 参考这个连接