悬停时检查鼠标按钮是否按下?
我在网站上有一个与用户互动的盒子网格.如果他们单击一个框,它将更改颜色.有很多盒子,我希望它不那么乏味,因此最好具有以下功能:如果按下鼠标按钮并将鼠标悬停在盒子上,它将改变状态.有什么想法吗?
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?
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.
例如,要检测用鼠标输入元素时是否按下了左键,可以使用:
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
}
})
以下是此想法的演示: http://jsfiddle.net/Ah6pw/
Here is a demonstration of this idea: http://jsfiddle.net/Ah6pw/
按住鼠标左键,然后在不同的列表项中移动鼠标.
Hold down the left mouse button and move your mouse through different list items.