简体   繁体   English

如何区分滚动事件和长按事件?

[英]How to differentiate between scroll event and long press event?

Problem :问题 :

SomeDomElement.addEventListener('touchstart', function preventLongPress(event) {
              
                  if (event.touches.length >=1) event.preventDefault();
                }, false);

If I use : if (event.touches.length >=1) event.preventDefault();如果我使用: if (event.touches.length >=1) event.preventDefault(); then this prevents long press event but also disables the scroll event.那么这可以防止长按事件,但也会禁用滚动事件。

There is no touchmove or touchend events for long press.长按没有touchmovetouchend事件。

What I desired :我想要的:

prevent long press but don't prevent scrolling防止长按但不防止滚动

N ote : I am using vanilla Javascript only , no jQuery注意我只使用 vanilla Javascript,没有使用 jQuery

Hope this will help you.希望这会帮助你。

document.addEventListener("touchstart", function(){
    detectTap = false;
});
document.addEventListener("touchmove", function(){
    detectTap = true;
});
document.addEventListener("touchend", function(){
    if(detectTap)
        alert("scrolled"); /* here add whatever functionality you wants */
    else 
        alert("long pressed"); /* here add whatever functionality you wants */
});

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

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