简体   繁体   English

"setPointerCapture 在 Chrome 和 Firefox 中的行为不同"

[英]setPointerCapture behaves differently in Chrome and Firefox

I have a bunch of square div inside flexboxes.我在 flexbox 中有一堆方形 div。 When I press a mouse button inside a square, I would like the background of the target square to change.当我在正方形内按下鼠标按钮时,我希望目标正方形的背景发生变化。 I would like to capture the mouse, so when I move the mouse outside the captured square and release the mouse button the background should change back to its original color.我想捕获鼠标,所以当我将鼠标移到捕获的正方形外并释放鼠标按钮时,背景应该变回原来的颜色。

This is a little difficult to describe, so here's the code.这有点难以描述,所以这里是代码。 https://codepen.io/tqphan/pen/qBWaVod https://codepen.io/tqphan/pen/qBWaVod

window.addEventListener('mousedown', function(e) {
  e = e || window.event;
  var target = e.target || e.srcElement;
  target.classList.add("pressed")
  target.setPointerCapture(e.pointerId);
}, true);
window.addEventListener('mouseup', function(e) {
  e = e || window.event;
  var target = e.target || e.srcElement;
  target.classList.remove("pressed")
  target.releasePointerCapture(e.pointerId);
}, true);

To reproduce the problem, please follow these steps:要重现此问题,请按照下列步骤操作:

  1. Press the left mouse button over a square.在正方形上按鼠标左键。
  2. While the left mouse button is held down, drag the mouse outside the square.在按住鼠标左键的同时,将鼠标拖到方块外。
  3. Release the left mouse button.松开鼠标左键。

In Firefox, it works as I expected.在 Firefox 中,它按我的预期工作。 In Chrome, the background doesn't change back to its original color.在 Chrome 中,背景不会变回原来的颜色。

I tried capturing the events for window and document.我尝试捕获窗口和文档的事件。

edit: It appears pointer capture and release are not executed in Chrome.编辑:似乎指针捕获和释放没有在 Chrome 中执行。

pen.js:6 Uncaught DOMException: Failed to execute 'setPointerCapture' on 'Element': No active pointer with the given id is found.

pen.js:12 Uncaught DOMException: Failed to execute 'releasePointerCapture' on 'Element': No active pointer with the given id is found.

pointerId is a property of the PointerEvent interface. pointerIdPointerEvent接口的属性。

MouseEvents like mousedown doesn't inherit from PointerEvent and doesn't have a pointerId property. 像mousedown这样的MouseEvents不会从PointerEvent继承,也没有pointerId属性。

What you want is to listen for pointerdown and pointerup events: 您想要的是监听pointerdownpointerup事件:

 onmousedown = e => console.log('mousedown', e.pointerId) // undefined onpointerdown = e => console.log('pointerdown', e.pointerId) // id 

That Firefox casts undefined to 0 while Chrome doesn't is an unfortunate discrepancy, but ultimately no browsers really did what you expect, since your code was broken here. 不幸的是,Firefox将undefined0而Chrome并非如此,但是最终,没有浏览器真正做到了您的期望,因为您的代码在这里被破坏了。

I had the same or similar problem, setPointerCapture() worked perfectly for my app on FireFox, but not on Chrome.我遇到了相同或类似的问题,setPointerCapture() 在 FireFox 上非常适合我的应用程序,但在 Chrome 上却不行。 On Chrome it seemed to do nothing, no error-messages either.在 Chrome 上,它似乎什么也没做,也没有错误消息。

I finally figured out why.我终于明白为什么了。 On Chrome setPointerCapture() seems to only capture "pointer events" meaning events named like 'pointermove' and 'pointerup, NOT events 'mousemove' and 'mouseup' etc. This happens on Chrome version 97.0.4692.99 (Official Build) (64-bit)在 Chrome 上 setPointerCapture() 似乎只捕获“指针事件”,这意味着事件名为“pointermove”和“pointerup”,而不是事件“mousemove”和“mouseup”等。这发生在 Chrome 版本 97.0.4692.99(官方构建)(64-少量)

On FireFox setPointerCapture() captures also<\/em> "mouse -events" like 'mousemove' and 'mouseup'.在 FireFox 上,setPointerCapture()还<\/em>捕获“鼠标事件”,例如“mousemove”和“mouseup”。 Those were the ones for which I had set event-handlers in my DOM-element.这些是我在我的 DOM 元素中设置事件处理程序的那些。 Therefore they worked fine on FireFox.因此他们在 FireFox 上运行良好。

The solution was to modify my code so that instead of adding handlers for ' mousemove<\/strong> ' and ' mouseup<\/strong> ' I now add handlers for ' pointermove<\/strong> ' and ' pointerup<\/strong> '.解决方案是修改我的代码,而不是为“ mousemove<\/strong> ”和“ mouseup<\/strong> ”添加处理程序,我现在为“ pointermove<\/strong> ”和“ pointerup<\/strong> ”添加处理程序。 After this change it works equally well on both FireFox and Chrome.在此更改之后,它在 FireFox 和 Chrome 上同样适用。

"

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

相关问题 setInterval()在Firefox和Chrome中的行为有所不同 - setInterval() behaves differently in Firefox and Chrome 按钮在Chrome和Firefox上的行为有所不同 - Button behaves differently on Chrome and Firefox 取消按钮在Firefox和Chrome中的行为有所不同 - Cancel button behaves differently in Firefox and in Chrome iframe 主体上的 ResizeObserver 在 Chrome 和 Firefox 上的行为不同 - ResizeObserver on iframe body behaves differently on Chrome and Firefox 在Flexbox中固定的位置在Chrome和Safari / Firefox之间的行为有所不同 - Position fixed in Flexbox behaves differently between chrome and safari / firefox 在iFrame中单击锚点在Firefox和Chrome中的行为有所不同 - Clicking an anchor inside an iFrame behaves differently in Firefox and Chrome 为什么document.write()在Firefox和chrome中表现不同? - Why document.write() behaves differently in Firefox and chrome? Javascript动画在更新的Chrome中的行为有所不同 - Javascript animation behaves differently in updated Chrome JavaScript替换在Chrome和IE中的行为有所不同 - JavaScript replace behaves differently in Chrome to IE Javascript日期构造函数在IE和Chrome中的行为有所不同 - Javascript Date constructor behaves differently in IE and Chrome
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM