【发布时间】:2011-06-18 00:21:31
【问题描述】:
jQuery 无限滚动插件有什么替代品吗?
http://www.beyondcoding.com/2009/01/15/release-jquery-plugin-endless-scroll/
【问题讨论】:
标签: jquery jquery-plugins scroll
jQuery 无限滚动插件有什么替代品吗?
http://www.beyondcoding.com/2009/01/15/release-jquery-plugin-endless-scroll/
【问题讨论】:
标签: jquery jquery-plugins scroll
这应该在没有插件的情况下做同样的事情
$(window).scroll(function () {
if ($(window).scrollTop() >= $(document).height() - $(window).height() - 100) {
//Add something at the end of the page
}
});
根据@pere 的评论,最好使用下面的代码来避免过多的事件触发。
灵感来自这个答案https://stackoverflow.com/a/13298018/153723
var scrollListener = function () {
$(window).one("scroll", function () { //unbinds itself every time it fires
if ($(window).scrollTop() >= $(document).height() - $(window).height() - 100) {
//Add something at the end of the page
}
setTimeout(scrollListener, 200); //rebinds itself after 200ms
});
};
$(document).ready(function () {
scrollListener();
});
【讨论】:
结合 Ergec 的回答和 Pere 的评论:
function watchScrollPosition(callback, distance, interval) {
var $window = $(window),
$document = $(document);
var checkScrollPosition = function() {
var top = $document.height() - $window.height() - distance;
if ($window.scrollTop() >= top) {
callback();
}
};
setInterval(checkScrollPosition, interval);
}
distance 是触发回调时距屏幕底部的像素数。
interval 是检查运行的频率(以毫秒为单位;250-1000 是合理的)。
【讨论】:
【讨论】:
我找不到完全符合我要求的,所以我从头开始构建了一个。它具有暂停功能,因此在您滚动时不会无休止地加载。有时有人可能想查看页脚。它只是附加一个“显示更多”按钮以继续附加。我还加入了 localStorage,因此当用户点击离开时,他们不会在结果中失去位置。
http://www.hawkee.com/snippet/9445/
它还有一个回调函数,可以调用它来操作新添加的结果。
【讨论】:
这应该解决用户在事件触发之前到达页面底部的错误。我在我的项目中看到了这一点,如果你已经在页面底部,你应该在初始化事件之前检查一下。
var scrollListener = function () {
executeScrollIfinBounds();
$(window).one("scroll", function () { //unbinds itself every time it fires
executeScrollIfinBounds();
setTimeout(scrollListener, 200); //rebinds itself after 200ms
});
};
$(document).ready(function () {
scrollListener();
});
function executeScrollIfinBounds()
{
if ($(window).scrollTop() >= $(document).height() - $(window).height() - 100) {
//Add something at the end of the page
}
}
【讨论】: