使用 HTML5 可以使元素“不可见”。用于拖/放交互?

发布于 2025-01-15 23:29:46 字数 1244 浏览 0 评论 0原文

仍在尝试 将拖放和移动元素与鼠标结合起来 当我'时,我正在努力解决 dragenterdragleave 父元素不被调用的问题米移动子元素。这看起来很自然,因为该元素始终悬停在父元素上并防止为父元素调用 dragenter

我尝试在 dragenterdragleavedragover 中调用 stopPropagation()preventDefault()子元素的 code>、dragstartdrag 事件,但没有实际效果。

另一个问题似乎解决类似的问题,但如果我正确理解的话,没有真正的解决方案。

也许兔子洞里太黑了,看不到明显的东西 - 如何防止拖动的项目避免调用其父级 dragenter/dragleave 事件?

在另一个层面上,我只想知道该元素是否已被删除到父元素之外(然后将其返回到原始位置)。有更简单的方法吗?

这是我当前的代码 - 在当前状态下,元素正在用鼠标移动,从而防止dragenterdragleave 被调用。

停用 draggable_element 的实际移动将使 dragenter 和 dragleave 在离开父/目标区域时以及在输入拖动的元素时被调用(我以某种方式无法避免)。

Still trying to combine drag&drop and moving an element with the mouse I'm struggling with dragenter and dragleave not being called for a parent element when I'm moving the child element. This seems quite natural because the element always hovers over the parent element and prevents dragenter being called for the parent.

I tried to call stopPropagation() and preventDefault() in dragenter, dragleave, dragover, dragstart and drag events for the child element but with no real effect.

Another question seems to address a similar issue but with no real solution if I get this correctly.

Maybe it's just too dark down here in the rabbit hole to see the obvious - how do I prevent the dragged item from avoiding its parents dragenter/dragleave events to be called?

On another level I just want to know if the element has been dropped outside the parent element (to then return it to it's original position). Is there an easier approach?

Here is my current code - In the current state the element is being moved with the mouse and thus preventing dragenter or dragleave being called.

Deactivating the actual movement of draggable_element will make dragenter and dragleave be called when leaving the parent/target area but also when the dragged element is being entered (what I somehow can't avoid).

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

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

发布评论

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

评论(1

独享拥抱 2025-01-22 23:29:46

找到了它 - 而不是摆弄拖/放事件,您只需为拖动的项目停用pointer-events,以避免不需要的dragenter/dragleave父级的事件,然后再次将其重新打开(默认情况下必须激活它才能首先启用拖动)。

draggable_element.addEventListener("dragstart", (e) => {
  e.srcElement.style.pointerEvents = "none";
  ... // rest of code
});

elem.addEventListener("dragend", (e) => {
  e.srcElement.style.pointerEvents = "auto";
  ... // rest of code
});

这是一个工作示例: https://jsfiddle.net/03a9s4ur/10/

Found it - instead fiddling with the drag/drop events you just have to deactivate pointer-events for the dragged item to avoid unwanted dragenter/dragleave events for the parent and turn it back on again afterwards (it has to be activated by default to enable dragging in the first place).

draggable_element.addEventListener("dragstart", (e) => {
  e.srcElement.style.pointerEvents = "none";
  ... // rest of code
});

elem.addEventListener("dragend", (e) => {
  e.srcElement.style.pointerEvents = "auto";
  ... // rest of code
});

Here is a working example: https://jsfiddle.net/03a9s4ur/10/

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