[英]Responding to touchstart and mousedown in desktop and mobile, without sniffing
So, I have a problem. 所以,我有一个问题。 I want to respond to a user pressing the mouse button (on desktop) or touching a div (on mobile).
我想对用户按下鼠标按钮(在桌面上)或触摸div(在移动设备上)做出回应。 I'm trying to be compatile with evergreen browsers.
我正在尝试与常绿的浏览器兼容。 This is what I tried so far:
这是我到目前为止尝试过的:
mouseDown
event. mouseDown
事件。 This works on desktop but doesn't work in mobile if the user is dragging. touchStart
event. touchStart
事件。 This works on mobile and desktop, except for Edge and Safari desktop, which don't support touch events. preventDefault
. preventDefault
。 This causes a double handler call on Chrome mobile. preventDefualt
has no effect on them . preventDefualt
没有任何影响。 What I get is a warning message saying "[Intervention] Unable to preventDefault inside passive event listener due to target being treated as passive. See https://www.chromestatus.com/features/5093566007214080"
in the console, preventDefault
is ignored and my event is called twice. "[Intervention] Unable to preventDefault inside passive event listener due to target being treated as passive. See https://www.chromestatus.com/features/5093566007214080"
,在控制台中将preventDefault
忽略并我的活动叫了两次。 Obviously this can be solved by sniffing touch events, but the net is full of self-righteous rants on how one has to be device-agnostic and that it's dangerous to detect touch events before the user interacted . 显然,这可以通过嗅探触摸事件来解决,但是网络上充满了自以为是的声音,说明人们必须如何与设备无关,并且在用户交互之前检测触摸事件很危险 。
So I guess that the question is: is there a way to do what I want to do without sniffing for touch events? 因此,我想问题是:是否可以在不嗅探触摸事件的情况下做我想做的事情?
Below my sample React code: 在我的示例React代码下面:
function handler(e) {
console.log('handler called')
e.preventDefault()
}
export default function MyElement() {
return (
<div
onMouseDown={handler}
onTouchStart={handler}
>
Hello
</div>
)
}
It turn out it's not yet possible in React . 事实证明,在React中还不可能 。 One workaround is set a flag the first time
touchStart
it's received. 一种解决方法是在第一次收到
touchStart
其设置为标志。
touchHandler = () => {
this.useTouch = true
this.realHandler()
}
mouseHandler = () => {
if (this.useTouch) return
this.realHandler()
}
With the caveat that the first touchStart
can be lost in case of dragging. 请注意,在
touchStart
可能会丢失第一个touchStart
。
Quite disappointing. 非常令人失望。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.