我想在用户到达屏幕底部之前执行滚动命令。下面的代码在用户向下滚动屏幕的中间位置时加载新的动态内容,但我希望动态内容加载到视口之外。有没有办法做到这一点?我是否需要调整.outerheight或.offset来提前加载动态内容?我附上了一个图形,显示了我正在努力实现的目标。
var busy = false;
jQuery( document ).ajaxStart(function() {
busy = true;
});
jQuery( document ).ajaxStop(function() {
busy = false;
});
jQuery(window).bind('scroll', function() {
if(jQuery(window).scrollTop() >= jQuery('#dynamicContent').offset().top + jQuery('#dynamicContent').outerHeight() - window.innerHeight) {
if (!busy) {
jQuery('.load-more').click();
}
}
});
下面是我的ajax代码片段:
$.ajax({
url: '/content',
type: 'post',
data: { categoryData: categoryData},
dataType: 'json',
success: function(res) {
response = res.data;
},
error: function(err) {
error = err;
},
complete: function() {
self.hideLoader();
if (!error) {
$('#dynamicContent').html(response.html);
self.filterSet(response.enabled);
self.setHistory(href, order, filters);
}
}
});
发布于 2018-02-27 19:43:59
当您到达页面底部时,要检测的示例代码
$(window).on("scroll", function() {
var scrollHeight = $(document).height();
var scrollPosition = $(window).height() + $(window).scrollTop();
if ((scrollHeight - scrollPosition) / scrollHeight === 0) {
// when scroll to bottom of the page
alert("you are at bottom of page");
}
});
https://stackoverflow.com/questions/49016443
复制