Chrome 似乎有 event.which === 1
在 mousemove
监听器是否在移动事件发生时当前按下了鼠标左键。
但是 Firefox 没有;它只设置 event.which
在mousedown
和mouseup
。我不知道其他浏览器。
有人能想出一种好方法来特征检测这种行为吗?
注意:我知道你可以使用 mousedown 和 mouseup 监听器来破解它来更新 mouseIsPressed
变量,但这在我的情况下不是一个好的黑客(iframed 窗口内的拖动交互,即如果发生在 iframe 外部,则无法捕获 mouseup),所以我想使用 native event.which
如果有的话。
最佳答案
DOM 级别 3 defines MouseEvent#buttons
,其中 Firefox supports 。来自 the MDN article on mousemove
:
The buttons being pressed when the mouse event was fired: Left button=1, Right button=2, Middle (wheel) button=4, 4th button (typically, "Browser Back" button)=8, 5th button (typically, "Browser Forward" button)=16. If two or more buttons are pressed, returns the logical sum of the values. E.g., if Left button and Right button are pressed, returns 3 (=1 | 2).
关于javascript - 如何在 mousemove 监听器中检测 event.which ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24872651/