2012-03-12 37 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(); 
    }