更好的方式使用mousedown和mousemove?

时间:2017-06-10 17:48:35

标签: javascript jquery coffeescript

我使用以下内容围绕中心点旋转元素。

$('.marker').on 'mousedown': ->
  $(this).on 'mousemove': ->
    rotateAnnotationCropper $('.innerCircle').parent(), event.pageX, event.pageY, $(this)

不确定是否需要这个,但它调用的功能如下。

rotateAnnotationCropper = (offsetSelector, xCoordinate, yCoordinate, markerNumber) ->
  x = xCoordinate - (offsetSelector.offset().left) - (offsetSelector.width() / 2)
  y = -1 * (yCoordinate - (offsetSelector.offset().top) - (offsetSelector.height() / 2))
  centerAngle = 90 - (Math.atan2(y, x) * 180 / Math.PI)
  rotate = 'rotate(' + centerAngle + 90 + 'deg)' + ' translateX(-50%)'

它有一个例外,就像我希望的那样。当我将光标移动到中心点时,我必须将光标准确地保持在元素上,否则运动将停止。关于如何保持元素移动的任何想法,即使光标延伸到元素之外?

我目前在5个元素上使用类.marker。

Codepen:https://codepen.io/DaveVan/pen/QvJORb

1 个答案:

答案 0 :(得分:0)

绑定到document以捕获外部事件。请参阅this updated pen

$('.marker').on 'mousedown': ->
  marker = this
  $(document).on 'mousemove': ->
    rotateAnnotationCropper $('.innerCircle').parent(), event.pageX, event.pageY, $(marker)
相关问题