日期:2014-05-16  浏览次数:20495 次

IE中 scroll事件会触发多次问题

$(window).scroll(function () {
        if ($(document).scrollTop() + $(window).height() > $(document).height() - 1) {
            alert(1);
        }
});
//经测试 在chrome,ff中测试 向下滚动指触发一次,但是在ie6~ie8中 会触发2次
//有什么好的解决办法吗

------解决方案--------------------
加一个setTimeout试试。


var scrollTimer;
$(window).scroll(function () {
    if(scrollTimer) {
        clearTimeout(scrollTimer);
        scrollTimer = undefined;
    }
    scrollTimer = setTimeout(function(){
        if ($(document).scrollTop() + $(window).height() > $(document).height() - 1) {
            alert(1);
        }
    },300);
});

------解决方案--------------------
和resize事件一样,ie有这个问题,方法就是2#的用计时器来延时执行,然后clear掉最早一次的计时器