AngularJS, multiple draggable items causes 'jump' on initial move

只谈情不闲聊 提交于 2019-12-12 10:26:56

问题


I am using the example code located on the AngularJS main site. When I move the first draggable object, all is well. As soon as I begin dragging a second object, the second object 'jumps' exactly the distance which the first object was moved.

Initially I thought the fix would be as simple as resetting the variables. Unfortunately, all of my attempts have caused 'indentation errors'.

# 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:


It sounds like resetting x and y in the mousedown even would fix it:

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

If you're still getting indentation errors, make sure you aren't mixing tabs and spaces in your indentation.



来源:https://stackoverflow.com/questions/15227715/angularjs-multiple-draggable-items-causes-jump-on-initial-move

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!