jQuery Droppable:放置在 div 之外?
当您将项目拖到可放置 DIV 之外时,有什么方法可以让 jQuery 执行某个功能吗?
假设我们在 div 中有可拖动或可排序的项目,并且您希望在将它们拖放到其父 div 之外时将其删除。
我知道有“out”事件,但是当您将项目拖到 div 之外时,该事件就会起作用,而不是在您放下它时(释放鼠标按钮)。
Is there any way I can get jQuery to perform a function when you drag an item outside a droppable DIV?
Let's say that we have draggable or sortable items in a div and you want them deleted if they are dropped outside their parent div.
I know there is the "out" event but that works as soon as you drag the item outside the div, not when you drop it (release mouse button).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
终于找到了一个正确的解决方案,尽管它也有点“hacky”。我所做的是将可拖动的前景 div 设置为可放置的,但使用 dropover 和 dropout 功能来确定该项目是否位于前景 div 之外。当它位于 div 外部时,我绑定 mouseup 事件来执行放置功能。当它回来时,我取消绑定 mouseup 事件,这样它就不会触发。
因为拖动时我已经按住了鼠标按钮,释放它就相当于放下我“手中”的东西。
Finally got a proper solution to this, even though it is a bit "hacky" as well. What I do is I set the foreground div with the draggables as the droppable, but use the dropover and dropout features to determine if the item is outside the foreground div. When it's outside the div, I bind the mouseup event to do the drop functionality. When it's back in I unbind the mouseup event so it doesn't fire.
Because when dragging I'm already holding the mouse button down, releasing it is the same as dropping what I have in my "hand".
为什么不使用另一个可放置的 div 包装所有元素,然后在那里进行检查?
如果用户将其放到浏览器窗口之外怎么办?你也会考虑这个吗?
Why not wrapped all elements with another droppable div, then do the checking there?
What about if the user dropped it outside the browser window? Would you consider this also?
以一种相当“hacky”的方式解决了这个问题:制作一个表格,其中一个内容 div 位于中心,并使表格的所有其他单元格可放置。本质上,这意味着您可以毫无问题地将其放在中心 div 之外,但这仍然远不是一个好方法。
如果有人有更好的方法,请告诉我。
Solved this in a pretty "hacky" way: made a table with a content div in the center and made all other cells of the table droppable. Essentially this means you can drop outside the center div without problems, but it is still far from a good way to do it.
If someone has a better way, please tell me.