gpt4 book ai didi

javascript - 无法检测到鼠标移动时按下的鼠标按钮?

转载 作者:行者123 更新时间:2023-11-28 07:55:20 27 4
gpt4 key购买 nike

录制onmousedownonmouseup以在onmousemove中使用不起作用,因为onmouseup仅在按钮时触发一次发布到窗外:http://jsfiddle.net/f1nqproy/5/

event.button 仅在 Internet Explorer 中返回有意义的结果。

event.buttons 仅存在于 Firefox 中。

那么使用其他浏览器怎么办?

编辑:
MouseEvent.buttons 现在已经标准化,所以这个问题已经解决了: https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons

最佳答案

如果你想做一些与浏览器无关的事情,最简单的方法是使用 jQuery

jQuery is a fast, small, and feature-rich JavaScript library. It makes things like HTML document traversal and manipulation, event handling, animation, and Ajax much simpler with an easy-to-use API that works across a multitude of browsers. With a combination of versatility and extensibility, jQuery has changed the way that millions of people write JavaScript.

您可以轻松绑定(bind)到mouseup , mousedown事件并忘记浏览器兼容性

$( window).mouseup(function(e){
console.log("mouseup:", e.button);
});
$( window).mousedown(function(e){
console.log("mousedown:", e.button);
});

-----更新------

IE(像往常一样)做了另一项工作,因此如果指针位于窗口之外,您将不会收到 mouseup 事件。

一个技巧是使用 mouseleave 事件:

//hack for IE
$( window).mouseleave (function(e){
console.log("mouseleave");
});

JsFiddle

关于javascript - 无法检测到鼠标移动时按下的鼠标按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26162440/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com