I think the problem you are having is with your understanding of the way the event handlers work, once you have added an event handler it will listen out for its event.
So your code will add an event handler to listen for mousedown
when the dom is ready, once it occurs it will add an event for mousemove
- the document now has both event handlers registered so it will do stuff for both independently.
What you want to do, is remove the event handler for the mousemove
on the mouseup
event. That way the document now no longer listens to the mousemove
event handler because its been removed.
$('#box').mousedown(function(){
$(document).bind('mousemove',function(e){
// Do something:
});
});
$(document).mouseup(function(){
$(document).unbind('mousemove');
});
Here is a simple example, so you can see whats happening it will add a message under the box
.