【问题标题】:Detect Scroll event while already on top (Mobile)检测滚动事件,同时已经在顶部(移动)
【发布时间】:2017-10-26 09:31:11
【问题描述】:

当用户已经在最顶部并想要进一步向上滚动时,我正在尝试启动一个操作。

我设法使用普通计算机的轮子事件来做到这一点,但这不适用于手机。有没有办法追踪它?

js fiddle

$ = jQuery;
$(document).ready(function() {
  $('.foo').bind('mousewheel', function(e) {
    if (e.originalEvent.wheelDelta / 120 > 0 && document.documentElement.scrollTop === 0) {
      console.log('OBOVE THE TOP')
    }
  });
});
.foo {
  height: 4000px;
  background-color: green;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="foo">

</div>

【问题讨论】:

  • 我想你会用Touch Event找到你要搜索的东西

标签: javascript jquery html css scroll


【解决方案1】:

您可以使用 scrollTop 来检测页面上的视口位置,而不是使用鼠标滚轮。

祝你好运!

<html>
    <head>
        <script src="https://code.jquery.com/jquery-1.11.1.min.js"></script>
        <style>
        body{
            height: 5000px;
        }
        </style>
    </head>
    <body>
        Hi!
        <script>
        jQuery(window).on('scroll' , function(){
            var height = $(this).scrollTop();
            if(height < 0){
                console.log('ABOVE THE TOP');
            }
            if(height > 0){
                console.log('Below THE TOP');
            }
            if(height == 0){
                console.log('At the top');
            }
        });
        </script>
    </body>
</html>

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-17
相关资源
最近更新 更多