简体   繁体   English

iOS Safari:“ mousemove”上有事件监听器时不会触发“ click”事件

[英]iOS Safari: 'click' event not triggerd when there is an event-listener on 'mousemove'

on safari in iOS when an element or an ancestor of it has an event listener on mousemove then the click event listener on it is ignored. 在iOS的safari中,当元素或元素的祖先在mousemove上具有事件侦听器时,将忽略其上的click事件侦听器。

<div id="el"></div>

document.getElementById("el").addEventListener("mousemove", _e => console.log("mousemove"));
document.getElementById("el").addEventListener("click",     _e => console.log("click"));

on ios the click is never logged, on android (chrome) or desktop browsers, both are. 在ios(无论是在android(chrome)还是台式机浏览器上)上,都永远不会记录该点击。

there are dozens stackoverflow questions regarding ios click bugs, but i couldn't find any about this combination. 关于iOS点击错误,有数十个stackoverflow问题,但我找不到关于此组合的任何信息。

The following is an excerpt from Apple's own document describing how Safari on iOS uses touch inputs to emulate mouse events: 以下是Apple自己的文档的摘录,其中描述了iOS上的Safari如何使用触摸输入来模拟鼠标事件:

...events arrive in this order: mouseover , mousemove , mousedown , mouseup , and click . ...事件按此顺序到达: mouseovermousemovemousedownmouseupclick [...] if the contents of the page changes on the mousemove event, no subsequent events in the sequence are sent [...]如果在mousemove事件上页面的内容发生更改,则不会发送序列中的后续事件

https://developer.apple.com/library/content/documentation/AppleApplications/Reference/SafariWebContent/HandlingEvents/HandlingEvents.html https://developer.apple.com/library/content/documentation/AppleApplications/Reference/SafariWebContent/HandlingEvents/HandlingEvents.html

In other words, if you do some alteration to the DOM in your mousemove event handler (and not just a simple console.log() ), the click event will be deferred, according to Apple's spec. 换句话说,如果您在mousemove事件处理程序中对DOM进行一些更改(而不仅仅是简单的console.log() ),则根据Apple的规范,将推迟click事件。
Back when the iPhone was introduced, this was Apple's way of dealing with the fact that an actual mouse pointer can be used for hover effects while a touch can not. 早在推出iPhone时,这就是Apple处理事实的一种方式,即可以将实际的鼠标指针用于悬停效果,而触摸则不能。

Though, I'm not sure why the click event isn't deferred in the simple fiddle. 不过,我不确定为什么在简单的提琴中不会延迟click事件。 It could be that the algorithm used is more "clever" these days and determines if a DOM alteration is relevant by assessing what changed and factoring in contextual information. 这些天使用的算法可能更“聪明”,并通过评估发生了什么变化并考虑上下文信息来确定DOM更改是否相关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM