gpt4 book ai didi

javascript - document.elementFromPoint(x, y) 未报告正确的子元素

转载 作者:行者123 更新时间:2023-12-03 00:06:52 33 4
gpt4 key购买 nike

According to this answer ,我应该能够获取元素“放置”发生的元素。然而,它报告的结果非常奇怪和随机,这些结果似乎总是父元素,而不是拖动元素放置在其上的实际子元素。

Here's the FULL CODE EXAMPLE on CodeSandbox相关片段如下所示...

<div>
<div id="sidebar">
<drag :transfer-data="{ type: 'textbox', width: 330, height: 50 }" class="draggable-item"
>Textbox
<div slot="image" class="drag-image"><div class="field-container" style="width: 330px;height: 50px;">Texbox</div></div></drag
>
</div>
<div id="pages-container" @click.self="deselectAll();">
<b-alert show variant="info" class="text-center">Drag controls from the left sidebar to the pages below.</b-alert>
<drop
class="page-container"
v-for="(p, index) in pages"
@drop="onDrop"
@click.self="deselectAll();"
:data-page-number="p.pageNumber"
:key="p.pageNumber + '_page_' + index"
:style="{ width: p.width + 'px', height: p.height + 'px' }"
>
<img src="https://www.kirupa.com/flash/images/single_column_text.png" width="100%" height="100%" :data-page-number="p.pageNumber" @click.self="deselectAll();" />
<div style="position: absolute;top:0;bottom:0;left:0;right:0;background-color:white;opacity:0" :data-page-number="p.pageNumber" @click.self="deselectAll();"></div>

<vue-draggable-resizable
:id="f.id"
:style="{ 'z-index': '1044 !important' }"
:class="{ 'field-container grabbable': true, selected: f.isSelected }"
v-for="(f, f_index) in getPageFields(fields, p.pageNumber)"
:key="f.id"
:min-width="20"
:min-height="20"
:x="f.left"
:y="f.top"
:w="f.width"
:h="f.height"
:parent="false"
:z-index="f.isSelected ? '1045 !important' : '1044 !important'"
:prevent-deactivation="true"
:active.sync="f.isSelected"
@resizestop="onResizeStop"
@dragstop="onDragStop"
@activated="onActivated(f.id);"
@clicked="onActivated(f.id);"
>id: {{ f.id }} <br />
page: {{ p.pageNumber }} fieldIndex: {{ f_index }} selected: {{ f.isSelected }}</vue-draggable-resizable
>
</drop>
</div>
</div>

此处显示了相关的 JavaScript(页码警报测试始终未定义)...

onDragStop(x, y) {
//...
const el = document.elementFromPoint(x, y);
alert("Dropped onto page number: " + el.dataset.pageNumber);
}

最佳答案

正如@Joao 指出的,document.elementFromPoint()需要视口(viewport)坐标,但是 VueDraggableResizable提供相对于初始放置元素的偏移坐标(基于其配置的锁定纵横比和边界)。您仍然可以通过从可拖动项目本身获取视口(viewport)坐标来获取视口(viewport)坐标。

步骤:

  1. 添加 ref <vue-draggable-resizable> (这将创建对可拖动对象的引用数组),并更新 dragstop -handler 还采用我们将与适用的 ref 一起使用的索引下一步:

    // template
    <vue-draggable-resizable
    v-for="(f, f_index) in ..."
    ref="draggable" 👈
    @dragstop="(x,y) => onDragStop(x,y,f_index)" 👈
    >

    // script
    onDragStop(x, y, 👉index) { ... }
  2. dragstop -处理程序,禁用pointer-events在引用的可拖动 $el 上(对应于上一步的索引),这样document.elementFromPoint()可以抓取可拖动视口(viewport)坐标下方的元素,我们将通过 getBoundingClientRect() 获得该坐标:

    onDragStop(x, y, index) {
    const draggable = this.$refs.draggable[index].$el;
    draggable.style.pointerEvents = 'none'👈

    const { left: dragX, top: dragY } = draggable.getBoundingClientRect();
    const targetElem = document.elementFromPoint(dragX, dragY);

    draggable.style.pointerEvents = 'auto'

    ...
    }

demo

关于javascript - document.elementFromPoint(x, y) 未报告正确的子元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54928835/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com