0

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

1 回答 1

2

这听起来像重置x,甚至会修复它ymousedown

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

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

于 2013-03-08T18:22:25.617 回答