I think the problem you are facing is to understand how the event handlers work, after adding the event handler he will listen to his event.
Thus, your code will add an event handler to listen to mousedown
, when dom is ready, as soon as it happens, it will add an event for mousemove
- both event handlers are now registered in the document so that it does something for both independently.
What you want to do is remove the event handler for mousemove
in the mouseup
event. Thus, the document no longer listens for the mousemove
event handler because it was deleted.
$('#box').mousedown(function(){ $(document).bind('mousemove',function(e){
Here is a simple example , so you can see what happens, it will add a message under box
.
source share