2012-03-12 75 views
0

我正在通過滾動實現按需加載數據。滾動加載數據點播

我已經在document.ready中爲div註冊函數。

數據只能在滾動到達最後時填充。所以要確定是否滾動到最後我使用下面的功能。

$("#tree").scroll(function() { 
       if (isScrolledToBottom(this)) { 

       } 
      }); 

但是函數沒有返回正確的值。我的意思是,當滾動到達最後時,它應該返回一個真正的值。

function isScrolledToBottom(object) { 
      return ($(object).attr('scrollHeight') - $(object).scrollTop() - 1) <= $(object).height(); 
     }; 
+1

我敢肯定,還有那些已經解決。 Google for'無限Ajax滾動' – kirilloid 2012-03-12 10:02:53

回答

1

試試這個:

return ($(object).attr('offsetHeight') + $(object).attr('scrollTop') >= $(object).attr('scrollHeight')); 
1

如果你想要做的無限滾動,在這裏看看我的回答是: How to load the web page content based on user scrolling

這個演示是在一定Ÿ滾動位置觸發。如果您希望在用戶到達特定物品時專門完成此操作,則可能需要查看Waypoints插件。

http://imakewebthings.com/jquery-waypoints/

無限滾動演示:http://imakewebthings.com/jquery-waypoints/infinite-scroll/

$('.theItems:last').waypoint(function(event, direction) { 
    //Ajax fetch here 
}); 
+0

您的代碼運行良好,但示例非常混亂。 – Dan 2012-05-03 12:43:09

+0

那麼,我相信這個概念本身對於那些不瞭解它的人來說有點混淆。你不明白什麼? – Dutchie432 2012-05-03 12:44:11

+0

我一直在尋找控制你的無限代碼示例的js,以瞭解內容實際上是如何加載的,以及在哪裏進入,只是在頁面底部注意到它,這有助於。 – Dan 2012-05-03 12:48:21

0
var scrollHeight = $(object).prop("scrollHeight"); // total scrollable height of the element 
    var scrollTop = $(object).scrollTop(); // how far you have come from the top while scrolling vertically 
    var height = $(object).height(); // the constant height of the portion in display at all times 

    if (scrollHeight === (scrollTop + height)) { 
     //console.log("fetching page"); 
     fetchNextPage(); 
    }