Skip to content

Instantly share code, notes, and snippets.

@MNBuyskih
Last active September 12, 2015 15:17
Show Gist options
  • Save MNBuyskih/d0ab8a08f7d27e2d6878 to your computer and use it in GitHub Desktop.
Save MNBuyskih/d0ab8a08f7d27e2d6878 to your computer and use it in GitHub Desktop.
$.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