That should do the trick. If you click on the button (.but) and drag, you can mouseup anywhere on the page and still fire the click event. If you mouseleave "body body" of the page and mouseup , the bind ed event is mouseleave unbind ed.
$('.but').mousedown( function(e) { if (e.which == 1) { var $this = $(this); $this.bind('mouseleave', function(){ $('body').one('mouseup', function() { $this.click(); }); }); $this.mouseup(function() { $(this).unbind('mouseleave'); }); } });
source share