2013-04-15 96 views
0

首先的滾動高度,我想知道這方面的區別:獲取頁面

- $(window).height() 

- $(document).height() 

- $(window).scrollTop() 

這些條款看上去有點類似於我,我無法理解他們之間的明顯差異。這裏是我的答案:

  • $(window).height():提供窗口,用戶可以看到的高度。

  • $(document).height():給出文檔的總高度。根據頁面上的內容,這可以高於/低於窗口高度。

  • $(document).scrollTop():給出窗口中滾動條的垂直位置。

真正的問題: 我想實現懶加載有點事,我需要打電話到服務器時滾動條已經從頁面底部交叉點200像素。我無法使用上述值來獲得此。

任何幫助,將不勝感激。

回答

0

最後,我想通了,什麼應該理解這些條款後計算。感謝答案。我的定義幾乎是正確的。

function (isScrollable) { 
    var isUserAtBottom = ($(window).height() + $(window).scrollTop() + 200 > $(document).height()); 
    if (isUserAtBottom) { 
    // Do something (Like Ajax call to server to fetch new elements) 
    return true; 
    } 
} 
+1

這與我的回答基本相同。 – Bill

3

窗口是您可以看到的區域 - 就好像您的屏幕是一個窗口,並且您正在瀏覽文檔。文檔是整個文檔 - 它可能比窗口更短或更長。

這是你需要數學:

if($(document).height() - $(document).scrollTop() < 200){ 
    //Do something 
} 
1
$(window).height(); // returns height of browser viewport 
$(document).height(); // returns height of HTML document 
$(window).scrollTop(); //Get the current vertical position of the scroll bar for the first    element in the set of matched elements or set the vertical position of the scroll bar for every matched element. 

$(window).scrollHeight(); //Height of the scroll view of an element; it includes the element padding but not its margin. 
+0

'$(window).scrollHeight()'那是什麼? – yckart