简体   繁体   English

jQuery上下滚动无法在移动设备上使用

[英]jQuery scroll down and up not working on mobile

I am using jQuery to detect when a user scrolls the mousewheel down or up. 我使用jQuery来检测用户何时向下或向上滚动鼠标滚轮。 here is my code: 这是我的代码:

$(window).bind('mousewheel DOMMouseScroll', function(event){
    if (event.originalEvent.wheelDelta > 0 || event.originalEvent.detail < 0) {
        // scroll up
    }
    else {
        // scroll down
    }
});

I just realized this doesn't work with mobile swiping up or down. 我只是意识到这不适用于手机向上或向下滑动。 Is there a way I can modify the code so that it works on mobile too? 有没有一种方法可以修改代码,使其也可以在移动设备上运行?

You can actually this using jQuery scroll function and compare the last scroll position to the current scroll position if it's lesser the user is scrolling down otherwise up 实际上,您可以使用jQuery滚动功能来进行此操作,并将最后一个滚动位置与当前滚动位置进行比较(如果较小,则用户向下滚动,否则向上滚动)

 var last_pos= 0; $(window).scroll(function () { var current_pos= $(this).scrollTop(); if (current_pos > last_pos) { alert('down'); } else { alert('up'); } last_pos = current_pos; }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <h1>test</h1><h1>test</h1><h1>test</h1><h1>test</h1><h1>test</h1><h1>test</h1><h1>test</h1><h1>test</h1><h1>test</h1> 

For mobile you have scrollstart and scrollstop, you don't have up or down so you have to do it yourself :P 对于移动设备,您可以使用scrollstart和scrollstop进行操作,而不必上下滚动,因此您必须自己进行:P

Check this: jQuery Mobile: Make the header hide on scroll down and show on scroll up 检查以下内容: jQuery Mobile:使标题在向下滚动时隐藏并在向上滚动时显示

You have a snippet there you can easily edit for what you need! 您有一个代码段,可以根据需要轻松编辑!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM