【问题标题】:Prevent touchstart event when scrolling/swiping on mobile devices在移动设备上滚动/滑动时防止 touchstart 事件
【发布时间】:2015-07-14 18:43:14
【问题描述】:
我有一个需要在移动设备上运行的网站。如果我在尝试向下滚动页面时触摸链接,它会触发 touchstart 事件(在大多数情况下加载新窗口,但在标题的情况下,会在菜单中导航)。我希望能够在不触发 touchstart 事件的情况下滚动。我怎样才能做到这一点?
【问题讨论】:
标签:
javascript
iphone
mobile
scroll
swipe
【解决方案1】:
我想出了一个适用于页面上大多数可点击项目的解决方案:
$(document).bind("touchstart", function (e) {
touchStartPos = $(window).scrollTop();
}).bind("touchend", function (e) {
var distance = touchStartPos - $(window).scrollTop();
if (distance > 20 || distance < -20) {
e.preventDefault;
}
});
我页面上的一些项目似乎没有被绑定,但除了执行一般 $(document).bind() 之外,您还可以根据需要专门绑定每个项目。