-1

When I click middle mouse button in a browser, it activates the scroll mode on browser with cursor like Scroll cursor. The scroll mode cancels on Esc key or next mouse click. It we left/right click when in scroll mode, mouse events fires in different orders. In IE, middle button up and next mouse down and up are not firing. In Chrome, the next mouse up event is not firing. Is there any way to cancel the scroll mode via javascript? Is there any way to get all events fired in order?

See Fiddler

var mouseDowns = 0;
var mouseUps = 0;
$('body').on('mousedown', 'div', function () {
    $('#logs').prepend('<br/>mousedown' + mouseDowns++);
});
$('body').on('mouseup', 'div', function () {
    $('#logs').prepend('<br/>mouseup' + mouseUps++);
});
0

2 Answers 2

3

Listen to the mousedown event and cancel the default behavior if middle-mouse button is detected (older browsers may use different index for middle-mouse button through the e.which property):

document.addEventListener("mousedown", function(e) {
  if (e.button === 1) e.preventDefault();
});

Sign up to request clarification or add additional context in comments.

Comments

0

Register to mouse wheel event as follows

[element].attachEvent("onmousewheel", MouseWheelHandler); // IE 6,7,8
[element].addEventListener("mousewheel", MouseWheelHandler, false); // IE9, Chrome, Safari, Opera
[element].addEventListener("DOMMouseScroll", MouseWheelHandler, false); // Firefox

and simply return false in the handler to cancel the standard behaviour.

For middle mouse button click,

$('body').on('mousedown', 'div', function () {
if(event.button==1) {
     event.preventDefault();
     return event.returnValue = false;
}});

2 Comments

Is it for midlle mouse wheel scroll? I'm asking about middle mouse button click, which activates a scroll mode in browsers.
that was for middle mouse scroll. For middle mouse button click, listen to mousedown event. Event data has information about which button was clicked - left, right or middle and cancel default behaviour when middle button clicked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.