Created
October 3, 2014 09:43
-
-
Save rd13/92c74b049c1d49854e20 to your computer and use it in GitHub Desktop.
Angular Draggable Directive
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
'use strict' | |
angular.module('ip') | |
.directive('draggable', ($document) -> | |
restrict: "EA" | |
link: (scope, element, attr) -> | |
pos_y = pos_x = drg_h = drg_w = 0 | |
mousemove = (event) -> | |
element.css | |
top: event.pageY + pos_y - drg_h + "px" | |
left: event.pageX + pos_x - drg_w + "px" | |
mouseup = -> | |
$document.unbind "mousemove", mousemove | |
$document.unbind "mouseup", mouseup | |
element.on "mousedown", (event) -> | |
event.preventDefault() | |
drg_h = element[0].offsetHeight | |
drg_w = element[0].offsetWidth | |
pos_y = element[0].offsetTop + drg_h - event.pageY | |
pos_x = element[0].offsetLeft + drg_w - event.pageX | |
$document.on "mousemove", mousemove | |
$document.on "mouseup", mouseup | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment