Detect when user scrolled down and reached center of page

匿名 (未验证) 提交于 2019-12-03 02:41:02

问题:

Currently I have the following code which detects when a user has started scrolling and if they've reached the center of the page.

$(window).scroll(function() {         if ($(window).scrollTop()  > $(window).height() / 2) {             //Load more posts         } }); 

However the problem I have is that whenever a user starts scrolling, the above script fires. So if a user scrolls up to view previous posts, this loads more posts. If a user is in the center and scrolls just a tiny bit, the script loads more posts.

My question is, is there a way to determine when a user has started to scroll down AND has reached the center of the newly resized page, because of more appended posts.

Thanks

回答1:

Try this

$(document).on("scroll.y", function(e) {   if ($("body").scrollTop() >= ( ($(e.target).height() / 2) -250) ) {        console.log("approximate center"); $(e.target).off("scroll.y")   };   return false }) 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!