2009-09-23 158 views
3

我有一個<div>元素的靜態高度和overflow: auto;樣式中的搜索結果列表。我只想加載前x個搜索結果(例如20個),並在用戶滾動到包含搜索結果的元素底部時加載另一個x結果。加載內容作爲元素滾動到視圖中

任何人都可以向我解釋我將如何做到這一點?我找到了幾個例子,但所有這些例子都使用整個文檔的滾動值,而不是一個<div>。我使用jQuery,如果它很重要。

回答

4

的聲音,我喜歡在接近結束時檢測滾動條的位置。 Found this當時在jquery組上搜索。其建議的解決方案,如果需要添加少量文檔:

$.fn.isNearTheEnd = function() { 
    // remember inside of $.fn.whatever = function() {} 
    // this is the jQuery object the function is called on. 
    // this[0] is DOMElement 
    return this[0].scrollTop + this.height() >= this[0].scrollHeight; 
}; 

// an example. 
$("#content").bind("scroll", function() { 
    if ($(this).isNearTheEnd()) // load some content 
}); 
+0

謝謝,那就是我一直在尋找的。 – Aistina 2009-09-23 11:46:08

0

如果你比較像你div的.top() + .height()到窗口的.scrollTop + .height那麼當你在該分區的底部,你可以告訴,然後觸發下一個內容加載...