[英]Cannot Detect When User Scrolls to the Bottom of Page
I am using the following code to detect when a user scrolls to the bottom of the page: 我正在使用以下代码来检测用户何时滚动到页面底部:
$(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
}
});
However, instead of telling me when I scroll to the bottom of the page, the if statement becomes true when I scroll all the way to the top. 但是,当我滚动到页面底部时,并没有告诉我,if语句变为真。 Why is this happening?
为什么会这样呢? Thanks so much.
非常感谢。 The reason I need to detect when the user scrolls to the bottom is so that I can dynamically load content from a server.
我需要检测用户何时滚动到底部,是为了可以从服务器动态加载内容。 I don't think this matter, but I am using Flask on the backend and the malfunctioning page is rendered as a template with jinja-2.
我认为这无关紧要,但是我在后端使用Flask,并且故障页面使用jinja-2渲染为模板。
_onScrollHandler(event){
if(event.target.scrollHeight - event.target.scrollTop === event.target.clientHeight){
console.log('Reacehd Bottom')
}
}
Call this function when onscroll event occurs and from event arg you can get the scrollHeight scrollTop and clientheight. 在发生onscroll事件时调用此函数,并且从事件arg中可以获取scrollHeight,scrollTop和clientheight。 Please try with this one
请尝试这个
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.