document.elementFromPoint(x,y)没有报告正确的子元素

时间:2019-02-28 15:14:50

标签: javascript vue.js vuejs2 drag-and-drop

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(页码警报测试始终为undefined)...

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

1 个答案:

答案 0 :(得分:1)

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

步骤:

  1. <vue-draggable-resizable>添加ref(这将创建对可拖动对象的引用数组),并更新dragstop处理程序以同时获取一个索引,下一步将与适用的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处理程序中,在引用的可拖动$el上禁用pointer-events(对应于上一步的索引),以便document.elementFromPoint()可以抓取可拖动视口坐标下面的元素,我们将使用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