如何防止 iOS 5 中 touchmove 事件的默认行为?

发布于 2024-12-09 10:32:04 字数 459 浏览 0 评论 0原文

我有一个基于 Web 的应用程序,其中包含一个用户可以用手指上下滚动的组件。我使用该事件的 preventDefault 方法来防止触摸移动在 iOS 设备上移动整个屏幕的默认行为。

不幸的是,这在我今天早上刚刚升级到的 iOS 5 中似乎不再起作用了。我必须假设 iOS 5 中的做法有所不同,但我还没有找到提供说明的资源。

更新#1:我无法找到我的具体问题的答案,但我可以稍微调整我的代码以使用 -webkit-overflow-scrolling 样式(设置为一个值的“触摸”)并实现时髦的惯性滚动功能(其中内容滚动得更快,具体取决于您滑动的速度,并且如果触及边界,则会“橡皮筋反弹”回来。看起来很酷......

更新#2:我现在还有另一个奇怪的问题。由于某些奇怪的原因,溢出滚动行为有时会混淆,您必须在包含元素上左右拖动手指才能使其内容上下移动,我还没有弄清楚为什么会发生这种情况 -有人有什么想法吗?

I have a web-based application that includes a component that the user can scroll up and down with their finger. I use the event's preventDefault method to prevent the default behavior where the touch move shifts the whole screen around on iOS devices.

Unfortunately this does not seem to work anymore in iOS 5 which I just upgraded to this morning. I have to assume that this is just done differently in iOS 5, but I have yet to be able to find a resource that provides instructions.

Update #1: I haven't been able to find an answer to my specific question, but I was able adjust my code a bit to use the -webkit-overflow-scrolling style (set to a value of "touch") and implement the snazzy inertial scrolling capability (where the content scrolls faster depending on the velocity of your swipe and will "rubber band bounce" back if it hits the boundaries. Pretty cool looking...

Update #2: I have another strange problem now. For some odd reason that overflow scrolling behavior gets mixed up sometimes whereby you have to drag your finger left and right across the containing element in order to make its contents move up and down. I have yet to be able to figure out why this happens - does anyone have any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

未蓝澄海的烟 2024-12-16 10:32:04

我找到了一个非常简单的解决方案。当事件击中允许滚动的元素时,只需标记该事件即可。在文档的事件侦听器上,只需检查是否设置了标志,并且仅在未设置标志时停止事件:

this.$scrollableEl.on('touchmove', function(event){
  event.comesFromScrollable = true;
  // when you have containers that are srollable but 
  // doesn't have enough content to scroll sometimes:
  // event.comesFromScrollable = el.offsetHeight < el.scrollHeight;
});

$(document).on('touchmove', function(event){
    if (!event.comesFromScrollable){
      event.preventDefault();
    }
});

您还可以使用 event.stopImmediatePropagation 相反,因此您不需要文档元素上的 eventListener,但这会破坏 zepto.js在我的例子中点击

this.$scrollableEl.on('touchmove', function(event){
  event.stopImmediatePropagation();
});

I found a very simple solution. When the event hits your element that is allowed to scroll, just flag the event. On the event listener on the document just check if the flag is set and only stop the event if the flag isn't set:

this.$scrollableEl.on('touchmove', function(event){
  event.comesFromScrollable = true;
  // when you have containers that are srollable but 
  // doesn't have enough content to scroll sometimes:
  // event.comesFromScrollable = el.offsetHeight < el.scrollHeight;
});

$(document).on('touchmove', function(event){
    if (!event.comesFromScrollable){
      event.preventDefault();
    }
});

You could also use event.stopImmediatePropagation instead, so you dont need the eventListener on the document element, but this breaks zepto.js tap in my case:

this.$scrollableEl.on('touchmove', function(event){
  event.stopImmediatePropagation();
});
何止钟意 2024-12-16 10:32:04

首先,我可以使用以下代码验证 e.preventDefault() 是否禁用了 iOS 5 中的所有滚动:

document.ontouchmove = function(e){ e.preventDefault(); }

然而,不幸的是,这会禁用 Overflow:scroll div 上的滚动。 (如果有人有一个使内部元素滚动启用的解决方案,请分享。)

关于 update#2,我注意到当可滚动元素嵌套在另一个可滚动元素(包括页面本身)中时出现奇怪的行为。有时,设备会犹豫用户打算滚动哪个元素。特别是我使用position:fixed注意到了这个问题。我的解决方案是确保主体具有 100% 的高度,并且可滚动元素尽可能使用position:absolute。

First, I can verify that e.preventDefault() disables all scrolling in iOS 5 using the following code:

document.ontouchmove = function(e){ e.preventDefault(); }

Unfortunately, however, this disables the scrolling on overflow:scroll divs. (If anyone has a solution that leaves the inner element scrolling enabled, please share.)

Regarding update#2, I have noticed strange behavior when there is a scrollable element nested in another scrollable element (including the page itself). Sometimes the device hesitates on which element the user intends to scroll. In particular I've noticed this problem using position:fixed. My solution was to make sure the body has 100% height and that the scrollable elements use position:absolute where possible.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文