如何以编程方式中止 jQuery 拖动操作?
如何以编程方式中止 jQuery 拖动操作?
使用 jQuery UI,用户开始拖动操作。拖动时,会发生异步事件,导致拖动的元素被删除(例如,基于计时器的刷新)。在刷新中,我想在刷新之前执行类似的操作,以避免已删除元素出现错误:
element.trigger('dragstop');
但这似乎不起作用。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我添加这个答案是因为我正在寻找同样的问题并且我想要一个简短的答案。
只需在拖动事件中返回
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.
在 jQuery UI 中,“按设计”[1] 不允许在拖动时正式取消拖动——我不同意这种情况,但事实就是如此。
如果您确实想要取消飞行中的阻力,则需要结合一些技巧 [2, 3]:
现在,这并不漂亮。但它有效。即使在将鼠标悬停在接受放置的情况下取消时也是如此。
玩得开心。
[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]:
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
正如@FerRory建议的那样,您可以利用
drag
事件。您可以利用
data()
方法来确定元素是否可拖动:然后在异步操作中,如果被拖动的元素是已删除的元素,则可以设置该数据(我假设您有一些用于将 DOM 元素与服务器中的数据关联起来的系统):
我已经更新了我的示例 此处。第一个
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: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):
I've updated my example here. The first
div
will be "deleted" after 5 seconds.这可以通过使用拖放事件的回调函数(返回 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