【发布时间】:2020-08-06 16:23:06
【问题描述】:
所以我一直在阅读这个问题,并看到了很多不同的解决方案。目前我的代码如下所示:
我正在使用这个插件:http://benalman.com/projects/jquery-throttle-debounce-plugin/
// Bind throttled scroll event to load new items when page is scrolled to the bottom.
$(window).data('loading', false).scroll($.throttle(800, Olemas.ScrollLoad));
Olemas.ScrollLoad = function() {
if ($(window).data('loading') == true) return;
if ($(window).scrollTop() >= ($(document).height() - $(window).height())) {
if (Olemas.NextPage <= Olemas.MaxPage) {
$(window).unbind('scroll'); // I have tried unbinding the event to stop it from launching
Olemas.LoadMoreItems(Olemas.DivToUpdate); // load the new items
$(window).data('loading', false).scroll($.throttle(800, Olemas.ScrollLoad)); // rebind scroll event.
}
}
};
问题是即使事件每 800 毫秒触发一次。当我快速滚动到底部时,滚动事件触发次数过多,页面加载错误。
if ($(window).scrollTop() >= ($(document).height() - $(window).height()))
在我的项目加载到页面后,此语句不应该通过,但在意识到页面高度已更改之前,它仍会通过几次。是否有可能是高度属性更新不够快,或者滚动事件已经以某种方式加载到 DOM 中?
在 LoadMoreItems 函数扩展页面后,如何阻止滚动事件触发。
编辑。
所以在这个函数中,警报会运行 2 次,然后在 LoadMoreItems ajax 成功中出现警报。
Olemas.ScrollLoad = function() {
/// <summary>
/// Loads more items when scrolled to the bottom.
/// </summary>
if ($(window).data('loading') == true) return;
if ($(window).scrollTop() >= ($(document).height() - $(window).height())) {
if (Olemas.NextPage <= Olemas.MaxPage) {
alert(Olemas.NextPage); // This runs 2 times, before the LoadMoreItems changes the NextPage value
$(window).off('scroll', $.throttle(800, Olemas.ScrollLoad));
Olemas.LoadMoreItems(Olemas.DivToUpdate);
$(window).data('loading', false).on('scroll', $.throttle(800, Olemas.ScrollLoad)); // rebind scroll event.
}
}
};
Olemas.LoadMoreItems = function () {
$(window).data('loading', true);
$.ajax({
url: Olemas.ActionLink,
data: { "page": Olemas.NextPage, "facId": Olemas.FacultyId },
success: function (data) {
$(".loadingImage").hide();
$(data).appendTo(Olemas.DivToUpdate);
alert("Done") // This is run after the previous alerts have fired 2 times.
Olemas.NextPage++;
Olemas.CheckPages();
}
});
return false;
};
【问题讨论】:
标签: jquery jquery-plugins jquery-pagination