将内容作为元素加载滚动到视图中

时间:2009-09-23 09:35:19

标签: javascript jquery ajax dynamic-loading

我在<div>元素中有一个搜索结果列表,其中样式中的静态高度为overflow: auto;。我想只加载前x个搜索结果(例如20个),并在用户滚动到包含搜索结果的元素底部时加载另外x个结果。

任何人都可以向我解释我会怎么做?我找到了一些示例,但所有这些示例都使用整个文档的滚动值,而不是单个<div>。我正在使用jQuery,如果重要的话。

2 个答案:

答案 0 :(得分:4)

听起来像你想要在接近结尾时检测滚动条的位置。在jquery组上搜索时Found this。它提出的解决方案,如果需要,可以添加一些文档:

$.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
});

答案 1 :(得分:1)

如果您将div的.top() + .height()与窗口的.scrollTop + .height进行比较,那么您可以告诉您何时位于该div的底部,然后触发下一个内容加载......

相关问题