iPhone 上的点击延迟和抑制输入焦点
iPhone 上的 webkit 浏览器在用户进行触摸和 javascript 获取单击事件之间有 300 毫秒的延迟。发生这种情况是因为浏览器需要检查用户是否进行了双击。我的应用程序不允许缩放,因此双击对我来说毫无用处。许多人针对这个问题提出了解决方案,他们通常涉及处理触摸结束事件上的“点击”,然后忽略浏览器生成的点击。但是,似乎无法抑制发送到输入元素的点击。如果您有一个在表单上方打开的对话框,然后用户单击关闭按钮,并且当表单消失时,他们的点击将路由到输入元素,这可能会导致问题。
jqtouch 示例(仅适用于 iPhone)
The webkit browser on iphone has a 300ms delay between when a user does a touch and when the javascript gets a click event. This happens because the browser needs to check if a user has done a double tap. My app doesn't allow zooming so a double tap is useless for me. A number of people have proposed solutions to this problem and they usually involve handling the 'click' on the touch end event and then ignoring the click generated by the browser. However, it doesn't seem to be possible to suppress a click that gets sent to an input element. This can cause a problem if you have a dialog that opens above a form then a user hits the close button and their click gets routed to an input element when the form disappears.
Example with jqtouch (for iphone only)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您想获得最快的响应速度,则必须在
touchstart
上捕获事件。否则你将注定会遇到这种输入延迟。您必须记住,虽然在 touchstart 上捕获事件并对其做出响应,但无法通过将手指拖出响应区域来取消操作。
我个人在我的基于 PhoneGap html/js 的 iphone 应用程序中使用了它,它运行得非常完美。提供这种近乎原生感觉的唯一解决方案。
现在关于您的问题 - 您是否尝试过阻止该事件的传播?它应该可以解决你的问题。
希望有帮助,
汤姆
You have to capture your event on
touchstart
if you want to get the fastest possible responsiveness. Otherwise you'll be doomed with this input lag.You have to remember though that capturing event on touchstart and responding to it makes it impossible to cancel action by dragging your finger out of responsive area.
I have personally used this in my PhoneGap html/js based iphone application and it worked perfect. The only solution to give this almost-native feel.
Now regarding your problem - have you tried to stop the propagation of the event? It should solve your problem.
hope it helps,
Tom
我和我的同事开发了一个名为 FastClick 用于消除 Mobile Safari 中的点击延迟。它将触摸转换为点击,并干净地处理
input
和select
元素的特殊情况。就像在正文上实例化它一样简单:
new FastClick(document.body)
,然后像往常一样监听点击事件。My colleagues and I developed an open source library called FastClick for getting rid of the click delay in Mobile Safari. It converts touches to clicks and handles those special cases for
input
andselect
elements cleanly.It's as easy as instantiating it on the body like so:
new FastClick(document.body)
, then listening for click events as usual.我将 Matt 的 FastClick 设为 jquery 插件:
stackoverflow 链接
刚刚有一条关于在没有传递必要范围的情况下调用 onClick 处理程序的评论。我更新了代码以使其正常工作。
当输入元素位于幽灵事件的位置下方时,似乎还会出现一个问题:焦点事件会被触发而不会被破坏。
I made Matt's FastClick a jquery plugin:
stackoverflow link
Just had a comment about the onClick handler being called without the necessary scope being passed. I updated the code to make it work.
There also seems to be a problem when input elements lie under the ghost event's position: the focus event gets triggered without being busted.
我在问题中看到两个问题。一是处理点击延迟,二是处理输入焦点。
是的,这两个问题都必须在移动网络中得到正确处理。
点击延迟有更深层次的原因。这篇文章很好地解释了 300 毫秒延迟的原因。
点击事件的响应能力。
值得庆幸的是,这个问题是众所周知的,并且已被许多库解决。
JQTouch、JQuery 移动,
JQuery.tappable,
Mootools-mobile,
和可点击
这些库中的大多数都会创建一个名为 tap 的新事件。您可以使用类似于单击事件的点击事件。这个 jquery-mobile 事件处理可能会有所帮助。
现在,第二个问题是输入焦点的处理。
这里也有明显的延迟。
这可以通过强制立即将焦点集中在 touchstart 或 touchend 事件之一的元素上来解决。这个 JQuery 事件处理可能会有所帮助。
您可以在“touchstart”事件处理中执行 e.stopPropagation 以避免传播。但我强烈建议不要返回 false;或 e.preventDefault,因为这会停止复制/粘贴、选择文本等默认功能。
I see two problems in the question. One is handling the delay in click and the other is handling input focus.
Yes, both of these have to be handled properly in mobile web.
The delay in click has deeper reasons. The reason for this 300ms delay is explained very well in this article.
Responsiveness of the click event.
Thankfully this problem is well known and solved by many libraries.
JQTouch, JQuery Mobile,
JQuery.tappable,
Mootools-mobile,
and tappable
Most of these libraries create a new event called tap. you can use the tap event similar to the click event. This jquery-mobile event handling might help.
Now, the second problem is with the handling of input focus.
There is a noticeable delay here also.
This can be solved by forcing focus on the element immediately for one of the touchstart or touchend events. This JQuery event handling might help.
You can do e.stopPropagation in 'touchstart' event handling to avoid propagation. But I would strongly advise against return false; or e.preventDefault as that would stop default functionality like copy/paste, selecting text etc.