简体   繁体   中英

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(); then this prevents long press event but also disables the scroll event.

There is no touchmove or touchend events for long press.

What I desired :

prevent long press but don't prevent scrolling

N ote : I am using vanilla Javascript only , no 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 */
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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