Created
September 21, 2012 02:40
-
-
Save brehaut/3759460 to your computer and use it in GitHub Desktop.
Trivial drag event registering function for jquery
This file contains hidden or 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
| 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