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?
Asked
Active
Viewed 1.8k times
22
-
There's some good answers here: http://stackoverflow.com/questions/2349764/jquery-is-mousedown-on-mouseover – MattDiamant Feb 26 '13 at 20:33
3 Answers
36
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
}
})
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.

Jonathan
- 8,771
- 4
- 41
- 78

Asad Saeeduddin
- 46,193
- 6
- 90
- 139
-
1@Jonathan true, but it will work fine in other browsers by using `.which` instead of `.buttons`. – Oak May 11 '15 at 07:39
-
@Jonathan I haven't tested extensively, but this appears to work for me in Firefox as well. – Asad Saeeduddin Nov 17 '16 at 11:40
-
See https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/buttons#Browser_compatibility for compatibility. – Adam Zerner Jun 17 '18 at 21:58
-
0
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)
Also these links could be usefull for you