当用户向下滚动并到达页面中心时检测 [英] Detect when user scrolled down and reached center of page

查看:108
本文介绍了当用户向下滚动并到达页面中心时检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有以下代码可以检测用户何时开始滚动,并且已经到达页面的中心。

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.

感谢

推荐答案

尝试这个

$(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
})

这篇关于当用户向下滚动并到达页面中心时检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆