已经在顶部检测滚动事件(移动)

时间:2017-10-26 09:31:11

标签: javascript jquery html css scroll

我尝试在用户已经位于最顶层并且想要进一步向上滚动时启动操作。

我设法使用正常计算机的滚轮事件,但不能使用手机。有没有办法跟踪?

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>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:0)

您可以使用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>
相关问题