2011-06-02 61 views
3

我有下面的Javascript,並且當滾動條打到頁面底部時,警報就會顯示出來。在滾動條到達底部之前做一些100px的事情

但是,我希望在之前發生100像素它到達底部。我會怎麼做?

$(window).scroll(function(){ 
    if($(window).scrollTop() == $(document).height() - $(window).height()){ 

    alert("at bottom"); 

    } 
} 

回答

9
$(window).scroll(function(){ 
    if($(window).scrollTop() + 100 > $(document).height() - $(window).height()){ 

    alert("at bottom"); 

    } 
}); 

使用>代替==因爲滾動事件觸發零星,所以你可能會滾過該值一堆次無當有一個精確匹配的情況下根本不會觸發。

+0

該解決方案在IE中多次觸發。 – usefulBee 2015-08-27 15:02:57

+0

@usefulBee是的,我將它留給開發人員來確定是否已採取計劃的行動。這個解決方案只是在您處於目標滾動位置時進行計算。 – RwwL 2015-08-28 09:23:01