我正在使用位于 AngularJS 主站点上的示例代码。当我移动第一个可拖动对象时,一切都很好。一旦我开始拖动第二个对象,第二个对象就会“跳跃”到第一个对象移动的距离。
最初我认为修复就像重置变量一样简单。不幸的是,我所有的尝试都导致了“缩进错误”。
# Angular Drag Components RE-uses vars from previous drag, bugging out the dragging
angular.module("aehalo", []).directive "draggable", ($document) ->
startX = 0
startY = 0
x = 0
y = 0
(scope, element, attr) ->
mousemove = (event) ->
y = event.screenY - startY
x = event.screenX - startX
element.css
top: y + "px"
left: x + "px"
mouseup = ->
$document.unbind "mousemove", mousemove
$document.unbind "mouseup", mouseup
element.css
position: "relative"
border: "1px solid red"
backgroundColor: "lightgrey"
cursor: "pointer"
element.bind "mousedown", (event) ->
startX = event.screenX - x
startY = event.screenY - y
$document.bind "mousemove", mousemove
$document.bind "mouseup", mouseup