How to capture click event with mouse scroll?

Asked

Viewed 188 times

3

My doubt is 'simple', but I can find no 'definitive' answer anywhere:

You can specifically capture the mouse scroll click with JS?

There is the function .click(), that I use a lot, but there is how to know which part of the mouse triggered the event?

My problem is that I am performing a validation to open a link in a new tab, I have already performed the validation of ctrlKey, and I need to find a solution to validate if the click was left (default) or if it was with scroll, because the same, by default, opens a new tab.

1 answer

4

Makes a function with mouseup (when it releases a mouse button), which when releasing any mouse button it captures the button who was released.

  • 0: Main button pressed, usually the left button or the uninitialized state
  • 1: Auxiliary button pressed, usually the wheel button or middle button (if any)
  • 2: Secondary button pressed, usually the right button
  • 3: Fourth button, usually the Back button of the browser
  • 4: Fifth button, usually the button Browser forward

inserir a descrição da imagem aqui

And if it is == 1 (scroll button that has been released) just do what you want:

document.addEventListener('mouseup',function(event){
     alert(event.button)
    if (event.button == 1) {
        //Fazer alguma coisa...
    }
})
<h2>Clique aqui em qualquer lugar </h2>

  • Perfect young man. Thank you very much!

  • @Diegofagundes, if this answer has solved your problem, you can mark it as accepted by clicking on the button of check in which is in the upper left corner of the response. :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.