I’m experimenting on a drag ‘n’ drop, this is my code here:
$('.box-title').live('mousedown click', function(e)
{
var self = $(this);
var Box = self.parent('#box');
if(e.type == 'mousedown')
{
$(window).mousemove(function(e)
{
Box.css({ cursor: 'move', top: e.pageY - 15, left: e.pageX - 125 });
});
}
else if(e.type == 'click')
{
Box.css({ cursor: 'default', top: e.pageY - 15, left: e.pageX - 125 });
}
});
On mousedown, it should initiate the dragging effect by moving the mouse, after that if I want to dock/drop the box where I want it to me, I click on it it should disable the moving but if I click on it, it doesn’t stop moving – just keeps following my mouse. How can you stop the dragging?
You need to unbind the
mousemovehandler which is still attached currently, for example:Or, in jQuery 1.4.3+ that
.live()handler can be a bit cleaner:As an aside, it appears you have multiple
id="box"elements in the page…make sure to use classes in those cases, in this code$(this).parent('#box')would be$(this).closest('.box')instead.