AngularJS,多个可拖动项目在初始移动时会导致“跳跃”

时间:2013-03-05 15:30:31

标签: javascript html5 dom angularjs coffeescript

我正在使用位于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

1 个答案:

答案 0 :(得分:2)

听起来好像重置xy mousedown甚至会修复它:

    element.bind "mousedown", (event) ->
      x = 0
      y = 0
      startX = event.screenX - x
      startY = event.screenY - y
      $document.bind "mousemove", mousemove
      $document.bind "mouseup", mouseup

如果您仍然出现缩进错误,请确保您没有在缩进中混合制表符和空格。