简体   繁体   中英

Prevent touchstart event when scrolling/swiping on mobile devices

I have a site that needs to work on mobile devices. If I touch a link while attempting to scroll down the page, it triggers the touchstart event (in most cases loading a new window, but in the case of the header, navigating through the menu). I want to be able to scroll without touchstart events being triggered. How can I accomplish this?

I've figured out a solution that works for most clickable items on the page:

$(document).bind("touchstart", function (e) {
    touchStartPos = $(window).scrollTop();
}).bind("touchend", function (e) {
    var distance = touchStartPos - $(window).scrollTop();
    if (distance > 20 || distance < -20) {
        e.preventDefault;
    }
});

A few items on my page seem to not get bound, but you can just specifically bind each item as needed in addition to doing a general $(document).bind().

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