javascript 悬停时检查鼠标按钮是否按下?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15098584/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check if mouse button is down while hovering?
提问by prismspecs
I have a grid of boxes that a user interacts with on a website. If they click a box, it changes color. There are quite a lot of boxes and I'd like it to be less tedious, so it would be nice to have the functionality be: if mouse button is down and you hover the box, it changes states. Any thoughts?
我有一个用户在网站上与之交互的框网格。如果他们单击一个框,它会改变颜色。有很多框,我希望它不那么乏味,所以功能是很好的:如果鼠标按钮被按下并且你悬停在框上,它会改变状态。有什么想法吗?
回答by Asad Saeeduddin
You can use the buttons
property on the event passed to the hover callback to check what mouse buttons were pressed when the event was triggered.
您可以使用buttons
传递给悬停回调的事件的属性来检查触发事件时按下了哪些鼠标按钮。
For example, to detect whether the left button was pressed when an element is entered with the mouse, you could use:
例如,要检测使用鼠标输入元素时是否按下了左键,您可以使用:
myElement.addEventListener("mouseover", function(e){
if(e.buttons == 1 || e.buttons == 3){
//do some stuff
}
})
Here is a demonstration of this idea: http://jsfiddle.net/Ah6pw/
这是这个想法的演示:http: //jsfiddle.net/Ah6pw/
Hold down the left mouse button and move your mouse through different list items.
按住鼠标左键并在不同的列表项之间移动鼠标。
回答by Stepo
I found something simmilar. Clicking the objects in some space and then little interaction. http://mrdoob.github.com/three.js/examples/canvas_interactive_cubes.html(look for inspiration into the code)
我发现了类似的东西。单击某个空间中的对象,然后几乎没有交互。 http://mrdoob.github.com/three.js/examples/canvas_interactive_cubes.html(在代码中寻找灵感)
Also these links could be usefull for you
这些链接也可能对您有用
回答by del
See onmousedown & onmouseupevents.