2012-04-02 47 views
1

可能重複:
Detecting when user scrolls to bottom of div with jQuery檢測,如果用戶已經滾動到一個div的底部

我有一個頁面,我有一個頭,左邊的菜單,內容,右鍵菜單和頁腳。在我的內容中,我有一個div(說id-foo),我有一些數據。當用戶滾動頁面並達到div的底部時,我必須進行一次ajax調用並帶來更多數據以填充該內容。我如何檢測用戶已滾動(頁面滾動,div中沒有​​滾動)到div(foo)的底部。

+4

第一個結果在谷歌搜索 - http://stackoverflow.com/questions/6271237/detecting-when-user-scrolls-to-bottom-of- div-with-jquery – ShankarSangoli 2012-04-02 21:10:31

+1

我的div沒有滾動。我已經明確提到它。 – 2012-04-02 21:11:53

+2

@RockySingh滾動'div'和滾動'body'有什麼主要區別? – kirilloid 2012-04-02 21:13:22

回答

2

根據當前屏幕大小和div位置,您可能需要自己做一些數學運算。該代碼將大致如下(未經測試):

// Cache these values. If the div size changes, divBottom will need to be recalculated. 
var $div = $("#yourdiv"); 
var divBottom = $div.offset().top + parseInt($div.height()); 

// Bind a scroll event for the whole page 
$("body").bind("scroll", function(e) 
{ 
    // Calculate how far down the user has scrolled 
    var screenBottom = $(this).scrollTop() + parseInt($(window).height()); 

    // Test if the div has been revealed 
    if(screenBottom > divBottom) 
    { 
     // do something 
    } 
});