How to find out the current state of the mouse button (mouse state or mouse state)

In Javascript, when I click on the scroll bar (Any scroll bar that appears on the page) and hover over the image, the image starts to drag again.

The image should be dragged only in the mouse state.

So, I tried to solve this problem, knowing the state of the mouse button (mousedown or mouseup)

This problem exists only in IE and Chrome , and not in other browsers that I tried.

JavaScript:

document.onmousemove = mouseMove;
document.onmousedown = mouseDown;
document.onmouseup   = mouseUp;

function mouseMove(ev) {

    var mouseButtonState;
    // put code here to get mouseButtonState variable
    console.log('state of mouse is'+mouseButtonState);
}

When I move the mouse, the program should show the desired value mouseButtonState up or down

How to find out the current state of the mouse button?

+5
3

, , ( ). , , ( ). , , , , .

Quirksmode ... , W3C , mousemove, , e.button reports 0 - , ? - e.button 0! ( W3C- ). , Internet Explorer:

http://www.quirksmode.org/js/events_properties.html#button

- - mousedown mouseup - , . "Touch", ... , . , , , .

, Internet Explorer:

document.body.onmousemove = function(e){
  e = e ? e : Event;
  if ( e.button == 1 ) {
    /// mouse button is pressed
  }
}

, IE :

var delm = document.documentElement ? document.documentElement : document.body;
var buttonPressed = false;

delm.onmousdown = function(e){
  buttonPressed = true;
}

delm.onmousup = function(e){
  buttonPressed = false;
}

delm.onmousmove = function(e){
  if ( buttonPressed ) {
    /// the button is pressed, but only whilst being 
    /// detected inside the browser
  }
}

, , , / , . Flash , , ...

, , , Internet Explorer , : . , , .

<img src="..." ondragstart="return false;" />
+4

, , , mouseup mousedown. blur, , . mouseup.

+1

Why not just go with a drag and drop jquery that can handle such events?

If you really want to link mouse and mousedown, you can use jquery

$(object).on("mouseup",function(){});
0
source

All Articles