On some condition I want to cancel onmousemove
event when mouse goes down, for example. Is it possible to determine the direction of onmousemove
event? jQ or JS is Ok.
I have drag’n’drop elements. The user drags the elenment up. If, for example, the bottom of the element reaches some position in the document (i.e. 500px
from the top of the document) the onmousemove
stops. And if the user will try to drag the element up again the function will not start. Only drag down will be possible for this element. So I thought it would be quite easy to do it by catching the direction of the mousemove
event. But it seems there’s no such kind of standard properties.
event.movementX is the difference in px from the previous positionX, e.g. 100 for right movement of 100 px, -100 for left movement etc, 0 for no movement.
There are standard properties that show deltas relevant to previous mouse move event:
Like said in the documentation:
You can save the position of the last
mousemove
event to compare to the current position:Here is a demo: http://jsfiddle.net/Dv29e/
Update
You can also throttle the
mousemove
event to get more of a general idea where the mouse has moved:This will only check the cursor's position against it's past position if:
mousemove_ok
variable is set totrue
which is done every half second.Here is a throttled demo: http://jsfiddle.net/Dv29e/4/