Last active
September 12, 2015 15:17
-
-
Save MNBuyskih/d0ab8a08f7d27e2d6878 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$.fn.drag = function () { | |
var $this = $(this); | |
$this.data('move', false); | |
$this.mousedown(function (e) { | |
e.preventDefault(); | |
$this.data('move', true); | |
$this.data('start', [e.offsetX, e.offsetY]); | |
}); | |
$(document).mouseup(function (e) { | |
e.preventDefault(); | |
$this.data('move', false); | |
}).mousemove(function (e) { | |
if ($this.data('move')) { | |
var startPos = $this.data('start'); | |
$this.offset({ | |
left: e.pageX - startPos[0], | |
top: e.pageY - startPos[1] | |
}); | |
} | |
}); | |
}; | |
// Example | |
$('#div').drag(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment