-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++);
});
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Sen Jacob
  • 3,384
  • 3
  • 35
  • 61

2 Answers2

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();
});
Community
  • 1
  • 1
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;
}});
Dhanunjai
  • 121
  • 4
  • Is it for midlle mouse wheel scroll? I'm asking about middle mouse button click, which activates a scroll mode in browsers. – Sen Jacob May 14 '15 at 04:28
  • 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. – Dhanunjai May 14 '15 at 06:33