gpt4 book ai didi

javascript - 在 jQuery 中检测滚动结束

转载 作者:行者123 更新时间:2023-11-30 14:06:50 29 4
gpt4 key购买 nike

我想检测用户是否滚动到窗口的末尾,然后触发点击“显示更多”按钮,如 Facebook 新闻源。

$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() == $(document).height()){
$('.feed-show-more-button').trigger('click');
}
});

这是我的代码,但只有当我滚动到窗口末尾然后返回顶部时它才有效

有人可以帮忙吗?

最佳答案

试试这个

var CheckIfScrollBottom = debouncer(function() {
if (getDocHeight() == getScrollXY()[1] + window.innerHeight) {
$('.feed-show-more-button').trigger('click');
}
}, 500);

document.addEventListener('scroll', CheckIfScrollBottom);

function debouncer(a, b, c) {
var d;
return function() {
var e = this,
f = arguments,
g = function() {
d = null, c || a.apply(e, f)
},
h = c && !d;
clearTimeout(d), d = setTimeout(g, b), h && a.apply(e, f)
}
}

function getScrollXY() {
var a = 0,
b = 0;
return "number" == typeof window.pageYOffset ? (b = window.pageYOffset, a = window.pageXOffset) : document.body && (document.body.scrollLeft || document.body.scrollTop) ? (b = document.body.scrollTop, a = document.body.scrollLeft) : document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop) && (b = document.documentElement.scrollTop, a = document.documentElement.scrollLeft), [a, b]
}

function getDocHeight() {
var a = document;
return Math.max(a.body.scrollHeight, a.documentElement.scrollHeight, a.body.offsetHeight, a.documentElement.offsetHeight, a.body.clientHeight, a.documentElement.clientHeight)
}

关于javascript - 在 jQuery 中检测滚动结束,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55212331/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com