Skip to content

Instantly share code, notes, and snippets.

@brehaut
Created September 21, 2012 02:40
Show Gist options
  • Select an option

  • Save brehaut/3759460 to your computer and use it in GitHub Desktop.

Select an option

Save brehaut/3759460 to your computer and use it in GitHub Desktop.
Trivial drag event registering function for jquery
function on_drag(el, handler) {
var el = $(el);
el.on("mousedown", function (e) {
var initial_x = e.clientY;
var initial_y = e.clientY;
function drag_handler(e) {
var delta_x = e.clientX - initial_x;
var delta_y = e.clientY - initial_y;
handler(delta_x, delta_y);
}
function drag_end () {
$(window).off("mousemove", drag_handler);
return false;
}
$(window).on("mousemove", drag_handler)
.one("mouseup", drag_end)
.one("blur", drag_end);
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment