Skip to content

Instantly share code, notes, and snippets.

@documentcloud
Created December 10, 2009 22:07
Show Gist options
  • Save documentcloud/253738 to your computer and use it in GitHub Desktop.
Save documentcloud/253738 to your computer and use it in GitHub Desktop.
$.fn.extend({
draggable : function() {
this.each(function() {
var drag = _.bind(function(e) {
e.stopPropagation() && e.preventDefault();
this.style.left = this._drag.left + event.pageX - this._drag.x + 'px';
this.style.top = this._drag.top + event.pageY - this._drag.y + 'px';
}, this);
var dragEnd = _.bind(function(e) {
e.stopPropagation() && e.preventDefault();
$(document.body).unbind('mouseup', dragEnd);
$(document.body).unbind('mousemove', drag);
$(this).removeClass('dragging');
}, this);
var dragStart = _.bind(function(e) {
e.stopPropagation() && e.preventDefault();
$(this).addClass('dragging');
this._drag = {
left : parseInt(this.style.left, 10) || 0,
top : parseInt(this.style.top, 10) || 0,
x : e.pageX,
y : e.pageY
};
$(document.body).bind('mouseup', dragEnd);
$(document.body).bind('mousemove', drag);
}, this);
$(this).bind('mousedown', dragStart);
});
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment