0
votes

Is there a way in JS to keep track of changing pageX and pageY while you move mouse and hold mouse button?

Original HTML DOM Event object has it's button property, but if I try to monitor it on mousemove, it always returns me zero (unlike ctrlKey, shiftKey etc., for which it will reflect change).

I tried jQuery Mousehold plugin, which waits for mousedown, sets interval, calls itself over and over and clears interval on mouseup or mouseout. It is good when you need to just catch mousehold, regardless of coordinates inside selector while button is held. But I need something to catch values during mousehold.

2

2 Answers

1
votes

You can set some flag variable on mousedown, and unset it in mouseup.
In mousemove handler simple check this variable.

1
votes

You could try using a mousemove event on body instead of the element you originally clicked. In your mousedown handler, set some global variable specifying which element is active and a global boolean to indicate dragging. Also set your mouseup handler on body. Keep in mind however that this method will fail if these events are handled by other elements on the page and they stop bubbling.