Javascript 检测页面滚动结束后的连续滚动
我正在尝试检测页面底部是否继续滚动 当到达页面末尾时,页面会显示更多信息,但我想显示的是,当人们在页面末尾滚动更多信息时 我可以检测滚动的结束,但我想检测人们是否继续滚动。Javascript 检测页面滚动结束后的连续滚动,javascript,html,scroll,lazy-loading,Javascript,Html,Scroll,Lazy Loading,我正在尝试检测页面底部是否继续滚动 当到达页面末尾时,页面会显示更多信息,但我想显示的是,当人们在页面末尾滚动更多信息时 我可以检测滚动的结束,但我想检测人们是否继续滚动。 函数滚动(){ 让scrollView=document.getElementById('field-scroll'); 如果($(滚动视图).scrollTop()+$(滚动视图).innerHeight()>=$(滚动视图)[0].scrollHeight){ 警报(“底部结束。但我希望在执行更多滚动时显示它,而不是
函数滚动(){
让scrollView=document.getElementById('field-scroll');
如果($(滚动视图).scrollTop()+$(滚动视图).innerHeight()>=$(滚动视图)[0].scrollHeight){
警报(“底部结束。但我希望在执行更多滚动时显示它,而不是立即显示”);
}
}
...
感谢您的帮助。您可以添加一个变量来指示首次滚动结束,然后检查该变量以了解用户是否继续滚动
function scrolling()
{
let scrollView = document.getElementById('field-scroll');
if($(scrollView).scrollTop() + $(scrollView).innerHeight()>=$(scrollView)[0].scrollHeight)
{
// first end of scroll
window.endOfScroll = true;
} else {
// unset the variable
window.endOfScroll = null;
}
}
此外,您还需要监听滚动事件。将这些添加到滚动
功能之外
const scrollView = document.getElementById('field-scroll');
// mouse wheel
scrollView.addEventListener("wheel", event => {
const direction = Math.sign(event.deltaY);
// if user is scrolling down at end of scroll
if (direction === 1 && window.endOfScroll) {
showMore();
}
});
// down arrow
scrollView.addEventListener("keydown", event => {
// if user is pressing down arrow at end of scroll
// 40 is the keycode for down arrow
if (event.keyCode === 40 && window.endOfScroll) {
showMore();
}
});
在窗口上关闭.scroll()事件,如下所示:
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() == $(document).height()) {
alert("bottom!");
}
});
贝扎德·法塔什(Behzad fartash)这能回答你的问题吗@约瑟夫:不,我的和你的评论一样。我想检测底部的“更多卷轴”。问题是,当到达底部时,onscroll对底部不起作用。(因为底部没有滚动高度。)这意味着,需要上下滚动。我认为,这对用户来说不是一个好的体验。。我只是想知道有没有什么“把戏”。顺便说一下,谢谢你的回答:)@EugeneFitzher我已经更新了答案。看看它是否能帮助你:)谢谢@EugeneFitzher很高兴它帮助了你。我以为向下滚动是-1,我的错。我会更新答案。
$(window).scroll(function() {
if($(window).scrollTop() + $(window).height() == $(document).height()) {
alert("bottom!");
}
});