13

如何以编程方式中止 jQuery 拖动操作?

使用 jQuery UI,用户开始拖动操作。在拖动时,会发生一个异步事件,该事件会导致被拖动的元素被删除(例如,基于计时器的刷新)。在刷新中,我想在刷新之前做这样的事情以避免被删除元素的错误:

   element.trigger('dragstop');

但这似乎不起作用。

4

4 回答 4

7

这可以通过使用拖放事件的回调函数(返回 false)来完成。见http://jqueryui.com/demos/draggable/#event-drag

于 2011-01-28T22:39:24.367 回答
4

我在寻找同样的问题时添加了这个答案,我想要一个简短的答案。

只需false在拖动事件中返回。

尽管我通过它们找到了我的答案,但还是感谢您的长篇回答。

于 2017-02-06T14:05:16.777 回答
2

Officially cancelling a drag while dragging is not allowed in jQuery UI "by design"[1] -- I disagree with the situation, but it is as it is.

If you somewhat reliably want to cancel a drag mid-flight, you will need to combine a couple of hacks [2, 3]:

$( window ).keydown( function( e ){
  if( e.keyCode == 27 ) {
    var mouseMoveEvent = document.createEvent("MouseEvents");
    var offScreen = -50000;

    mouseMoveEvent.initMouseEvent(
      "mousemove", //event type : click, mousedown, mouseup, mouseover, etc.
      true, //canBubble
      false, //cancelable
      window, //event's AbstractView : should be window
      1, // detail : Event's mouse click count
      offScreen, // screenX
      offScreen, // screenY
      offScreen, // clientX
      offScreen, // clientY
      false, // ctrlKey
      false, // altKey
      false, // shiftKey
      false, // metaKey
      0, // button : 0 = click, 1 = middle button, 2 = right button
      null // relatedTarget : Only used with some event types (e.g. mouseover and mouseout).
            // In other cases, pass null.
    );

    // Move the mouse pointer off screen so it won't be hovering a droppable
    document.dispatchEvent(mouseMoveEvent);

    // This is jQuery speak for:
    // for all ui-draggable elements who have an active draggable plugin, that
    var dragged = $('.ui-draggable:data(draggable)')
      // either are ui-draggable-dragging, or, that have a helper that is ui-draggable-dragging
      .filter(function(){return $('.ui-draggable-dragging').is($(this).add(($(this).data('draggable') || {}).helper))});

    // We need to restore this later
    var origRevertDuration = dragged.draggable('option', 'revertDuration');
    var origRevertValue = dragged.draggable('option', 'revert');

    dragged
      // their drag is being reverted
      .draggable('option', 'revert', true)
      // no revert animation
      .draggable('option', 'revertDuration', 0)
      // and the drag is forcefully ended
      .trigger('mouseup')
      // restore revert animation settings
      .draggable('option', 'revertDuration', origRevertDuration)
      // restore revert value
      .draggable('option', 'revert', origRevertValue);
  }
}

Now, this isn't pretty. But it works. Even when canceling while hovering an accepting droppable.

Have fun with it.

[1] - http://bugs.jqueryui.com/ticket/8414
[2] - https://gist.github.com/3517765
[3] - https://forum.jquery.com/topic/how-to-cancel-drag-while-dragging

于 2012-11-01T18:29:12.557 回答
1

正如@FerRory建议的那样,您可以利用该drag活动。

您可以利用该data()方法来确定元素是否可拖动:

$("div").draggable({
    drag: function() {
       return !$(this).data("disabledrag");
    },
    revert: true
});

然后在你的异步操作中,如果被拖动的元素是被删除的元素,你可以设置该数据(我假设你有一些系统用于将 DOM 元素与来自服务器的数据相关联):

var $dragging = $(".ui-draggable-dragging");
if ($dragging.length) {
    // If this is the item that was deleted, stop dragging:
    if ($dragging.attr("id") === "item-one") {
        $dragging.data("disabledrag", true);
        $dragging.addClass("deleted").html("This item has been deleted!");            
    }
}

我在这里更新了我的示例。第一个div将在 5 秒后“删除”。

于 2011-01-28T22:39:33.490 回答