Prevent touchmove default on parent but not child

后端 未结 5 2046
猫巷女王i
猫巷女王i 2020-12-15 17:22

I am creating a little web app for the iPad and I\'ve got several elements I am preventing the user from scrolling by preventing default on the touchmove event. However, I h

5条回答
  •  情深已故
    2020-12-15 17:52

    document.addEventListener('touchmove', function(e){e.preventDefault()}, false);
    document.getElementById('inner-scroll').addEventListener('touchmove', function(e){e.stopPropagation()}, false);
    

    The idea is that your main scroll is always (to your discretion) disabled, but in your inner-scroll you prevent the event from bubbling up (or propagating), so it will never reach the first event listener, which would ultimately cancel the touchmove event.

    I hope this is what you were looking for. I've had a situation similar to yours, where the main scroll was disabled on tablet, but i wanted an inner scroll to work. This seemed to do the job.

提交回复
热议问题