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

发布于 2024-10-14 13:19:36 字数 205 浏览 3 评论 0 原文

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

使用 jQuery UI,用户开始拖动操作。拖动时,会发生异步事件,导致拖动的元素被删除(例如,基于计时器的刷新)。在刷新中,我想在刷新之前执行类似的操作,以避免已删除元素出现错误:

   element.trigger('dragstop');

但这似乎不起作用。

How do I programmatically abort a jQuery drag operation?

Using jQuery UI, the user begins a drag operation. While dragging, an asynchronous event occurs that causes the dragged element to be deleted (a timer-based refresh, for example). In the refresh, I would like to do something like this before the refresh to avoid errors from the deleted element :

   element.trigger('dragstop');

But that didn't seem to work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

坚持沉默 2024-10-21 13:19:37

我添加这个答案是因为我正在寻找同样的问题并且我想要一个简短的答案。

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

感谢您的长篇回答,因为我通过它们找到了我的答案。

I add this answer as I was searching for the same problem and I wanted a short answer.

Just return false in the drag event.

Thank you to your long answer though as I found mine through them.

请持续率性 2024-10-21 13:19:37

在 jQuery UI 中,“按设计”[1] 不允许在拖动时正式取消拖动——我不同意这种情况,但事实就是如此。

如果您确实想要取消飞行中的阻力,则需要结合一些技巧 [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);
  }
}

现在,这并不漂亮。但它有效。即使在将鼠标悬停在接受放置的情况下取消时也是如此。

玩得开心。

[1] - http://bugs.jqueryui.com/ticket/8414
[2] - https://gist.github.com/3517765
[3] - https://forum.jquery.com/topic /如何在拖动时取消拖动

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

花海 2024-10-21 13:19:37

正如@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 秒后被“删除”。

As @FerRory suggests, you could take advantage of the drag event.

You could take advantage of the data() method to determine whether an element is draggable or not:

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

Then in your async action, you could set that data if the element being dragged is the one that's been deleted (I'm assuming you have some system for associating DOM elements with data from the server):

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!");            
    }
}

I've updated my example here. The first div will be "deleted" after 5 seconds.

深爱不及久伴 2024-10-21 13:19:36

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

This can be done by using the callback function (returning false) of the drag or drop event.
See http://jqueryui.com/demos/draggable/#event-drag

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文