简体   繁体   English

Javascript blur 和 pointerup 的侦听器在桌面和移动设备之间的工作方式不同

[英]Javascript Listeners for blur and pointerup working differently between Desktop and Mobile

I have a bit of Javascript that I'm using to delay the execution of some function after a blur event has been triggered by waiting for the next pointerup.我有一点 Javascript,我用它来延迟某些 function 的执行,因为等待下一个指针向上触发了模糊事件。

/* anonymous pointer listeners for debugging */
window.addEventListener("pointerdown", () => {console.log("pointerdown");});
window.addEventListener("pointerup", () => {console.log("pointerup");});

const handlePointerUp = () => {
    console.log("--- detected pointer up so remove the event listener");
    window.removeEventListener("pointerup", handlePointerUp);
}

const handleBlur = () => {
    console.log("--- focus lost so listen for next pointer up");
    window.addEventListener("pointerup", handlePointerUp);
}

document.getElementById("someButton").addEventListener("blur", handleBlur);

On Desktop this seems to work just fine and I get the console logs in the order that I expect在桌面上,这似乎工作得很好,我按照我期望的顺序获取控制台日志

// first time click on someButton
pointerdown
pointerup
// clicking away from someButton
pointerdown
--- focus lost so listen for next pointer up
pointerup
--- detected pointer up so remove the event listener

On Mobile the order is different在手机上顺序不同

// first time click on someButton
pointerdown
pointerup
// clicking away from someButton
pointerdown
pointerup
--- focus lost so listen for next pointer up

It is only triggering the blur after the touch event has been released and not at the start like it does with a mouse click, in which case it is listening for a pointer up that has already happened.它仅在触摸事件释放后触发模糊,而不是像单击鼠标那样在开始时触发模糊,在这种情况下,它正在侦听已经发生的向上指针。

Is there a way to achieve the same thing as what currently would happen on a Desktop in a way that would work the same on Mobile?有没有一种方法可以在移动设备上以同样的方式实现与当前在桌面上发生的事情相同的事情?

The events for pointer and mouse work differently.指针事件和鼠标事件的工作方式不同。 Quoting MDN;引用 MDN;

The pointerdown event is fired when a pointer becomes active.当指针变为活动状态时会触发 pointerdown 事件。 For mouse, it is fired when the device transitions from no buttons pressed to at least one button pressed.对于鼠标,当设备从未按下任何按钮过渡到至少按下一个按钮时触发。 For touch, it is fired when physical contact is made with the digitizer.对于触摸,当与数字化仪进行物理接触时会触发。

https://developer.mozilla.org/en-US/docs/Web/API/Element/pointerdown_event https://developer.mozilla.org/zh-CN/docs/Web/API/Element/pointerdown_event

I believe this is what's causing the effect.我相信这是造成这种影响的原因。 You can even emulate this by turning on "Toggle device toolbar" in chrome devtools.您甚至可以通过在 chrome devtools 中打开“切换设备工具栏”来模拟这一点。

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

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