javascript - 如何在 mousemove 监听器中检测 event.which ?

标签 javascript browser-feature-detection

Chrome 似乎有 event.which === 1mousemove监听器是否在移动事件发生时当前按下了鼠标左键。

但是 Firefox 没有;它只设置 event.whichmousedownmouseup 。我不知道其他浏览器。

有人能想出一种好方法来特征检测这种行为吗?

注意:我知道你可以使用 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/

相关文章:

javascript - 使用变量名 fadeIn 和 Prepend

javascript - 我如何以编程方式检测浏览器如何处理 window.close()?

javascript - Mo.js 对象扩展语法的替代方案

javascript - React 模态 - 不变违规

javascript - 使用 Modernizr 检测移动设备的最可靠方法是什么?

javascript - 使用 JavaScript 检测 WOFF 支持?

javascript - 特征检测、特征推断和使用 UA 字符串之间有什么区别

javascript - 我如何检测 :before pseudo-elements with javascript? 上的 CSS Transition 支持

javascript - 我想找到最有效的方法来填充车辆,基本上是不同的组合来满足车辆的容量

ASP.NET - 从本地资源文件中读取引用的文本