获取鼠标下的第二个元素,它不是第一个元素的父元素

随机的东西

我正在制作一个HTML5类似于 Trello的卡片拖放板

我正在将列表附加到列表插槽/容器。

当我onMouseDown在列表中时,列表会随着鼠标移动。我需要能够检查鼠标指针下方的列表容器onMouseUp这将允许我将列表附加到该容器。

目前我在尝试获取鼠标下的容器元素时遇到了困难。由于当时列表直接位于鼠标下方,因此我正在尝试查看鼠标下方的内容。

我无法获得我正在拖动的列表的父元素,因为父元素将始终返回原始列表父元素,因为我正在拖动列表并更改不会更改父元素的 Top 和 Left 属性。

所以基本上我需要查看鼠标下方的内容,不包括我正在拖动的列表。当我拖过列表插槽 1 时,我需要获取该元素,而当我拖过列表插槽 2 时,我需要获取该元素。

import React, { useEffect, useState } from "react";





export default function Main() {
  const [mousedown, setmousedown] = useState(0);
  const [etop, setetop] = useState(0)
  const [epostiontype, setepostiontype] = useState("relative")
  const [eleft, seteleft] = useState(0)

  ///////////////////
  let offsetX, offsetY;
  const move = e => {
    const el = e.target;
    el.style.left = `${e.pageX - offsetX}px`;
    el.style.top = `${e.pageY - offsetY}px`;
  };

  const add = e => {
    const el = e.target;
    offsetX = e.clientX - el.getBoundingClientRect().left;
    offsetY = e.clientY - el.getBoundingClientRect().top;
    el.addEventListener('mousemove', move);
  };

  const remove = e => {
    const el = e.target;
    el.removeEventListener('mousemove', move);
  };
  ///////////////////////
  const [list_states, setlist_states] = useState(
    [
      {
        name: "This is list 1",
        id: 1,
        top: 0,
        left: 0
      }
    ]
  );
  var getParents = function (elem) {

    // Set up a parent array
    var parents = [];

    // Push each parent element to the array
    for (; elem && elem !== document; elem = elem.parentNode) {
      parents.push(elem);
    }
    // Return our parent array
    return parents;

  };

  function mouse_moving_in_container(props) {
     // gets mouse position
     var x = props.pageX, y = props.pageY

     var element = document.elementFromPoint(x, y);

     console.log(element)

    // checks if mouse is down over a list. The mousedown var is the element id of the lsit that is being clicked. If no list is being clicked then the var is 0
    if (mousedown != 0) {
      var difference = props.pageY - document.getElementById("List_1").offsetTop
      var mouse_top = props.pageY - 10
      var mouse_left = props.pageX - 10
      setepostiontype("absolute")
      setetop(mouse_top)
      seteleft(mouse_left)


     
      // gets the element under the mouse
      var elementMouseIsOver = document.elementFromPoint(x, y)
      //returns array of all parents for the element above
      var element_parents = getParents(elementMouseIsOver)

      element_parents.forEach(element => {
      //  console.log(element.id)
     
        if (element.id.includes("Container")){
         // console.log("TThere is a <List>  container under mouse. ID: ")

        }
        else {
          //console.log("There is NO <List>  container under mouse.")
        }
        
      });
    }





  }



  return (
    <div className={"testing"}>
      <Container >
        <Slot>
          <List></List>
        </Slot>

        <Slot>

        </Slot>
      </Container>


    </div>


  );

  function change_mouse_status(event) {
    // console.log("change status",event)
    setmousedown(event)

  }




  function List() {
    return (
      <div
      
       id="List_1" className="width" style={{
        height: "100px",
        width: "180px",
        background: "red",


        position: `${epostiontype}`,

        // "relative",
        top: `${etop}`,
        left: `${eleft}`

      }}

        onMouseUp={() => change_mouse_status(0)}
        onMouseDown={() => change_mouse_status(1)}

      >
        this is a list
      </div>
    );
  }

  function Slot(props) {
    return (
      <div id="slot_1" style={{ display: "inline-flex", padding: "10px", margin: "10px", backgroundColor: "#e8e8ff", height: "200px", width: "200px" }}>
        {props.children}

      </div>
    );
  }

  function Container(props) {
    return (
      <div id="Container_1" onMouseMove={mouse_moving_in_container}
      name='testname'
        style={{
          display: "inline-flex",
          backgroundColor: "#94e49d38",
          height: "400px",
          width: "100vw-10px"
        }}
      >


        {props.children}

      </div>
    );
  }
}

这就是我迄今为止所拥有的。

任何助理将不胜感激。

随机的东西

我通过在pointerevents拖动时将插槽设置为无来解决了这个问题并在未拖动时设置回默认值。

我还将onMouseDownonMouseUp event处理程序更改为body页面的 ,以便仍然能够检测到event.

import React, { useEffect, useState } from "react";

export default function Main() {
  const [mousedown, setmousedown] = useState("0");
  const [etop, setetop] = useState(0)
  const [epostiontype, setepostiontype] = useState("relative")
  const [eleft, seteleft] = useState(0)
  const [slot_pointer_events, setslot_pointer_events] = useState("")


  const [list_states, setlist_states] = useState(
    [
      {
        name: "This is list 1",
        id: 1,
        top: 0,
        left: 0
      }
    ]
  );
  var getParents = function (elem) {

    // Set up a parent array
    var parents = [];

    // Push each parent element to the array
    for (; elem && elem !== document; elem = elem.parentNode) {
      parents.push(elem);
    }
    // Return our parent array
    return parents;

  };
  document.body.onmousemove = function (props) {
    var x = props.pageX, y = props.pageY

    // gets mouse position

    var element = document.elementFromPoint(x, y);

    //console.log(element)

    // checks if mouse is down over a list. The mousedown var is the element id of the lsit that is being clicked. If no list is being clicked then the var is 0
    if (mousedown != 0) {
      setslot_pointer_events("none")
      var difference = y - document.getElementById("List_1").offsetTop
      var mouse_top = y - 10
      var mouse_left = x - 10
      setepostiontype("absolute")
      setetop(mouse_top)
      seteleft(mouse_left)

      // gets the element under the mouse
      var elementMouseIsOver = document.elementFromPoint(x, y)
      //returns array of all parents for the element above
      var element_parents = getParents(elementMouseIsOver)

      console.log("this: ", elementMouseIsOver)


      element_parents.forEach(element => {
        //  console.log(element.id)

        if (element.id.includes("Slot")) {
          //   console.log("TThere is a <Slot>  container under mouse. ID: ")

        }
        else {
          //   console.log("There is NO <Slot>  container under mouse.")
        }

      });
    }

    else {

      setslot_pointer_events("")

    }
  }
  var mouseDown = 0;
  document.body.onmousedown = function (props) {
    var x = props.pageX, y = props.pageY
    var element = document.elementFromPoint(x, y);
    if (element.id.includes("List")) {

      setmousedown(element.id)
    }
    else {
      setmousedown("0")
    }
  }
  document.body.onmouseup = function () {
    setmousedown("0")
  }

  return (
    <div className={"testing"}>
      <Container >
        <Slot
          slot_id="Slot_1"
        >
          <List></List>
        </Slot>

        <Slot
          slot_id="Slot_2">

        </Slot>
      </Container>
    </div>
  );
  function List() {
    return (
      <div

        id="List_1" className="width" style={{
          height: "100px",
          width: "180px",
          background: "red",

          pointerEvents:
            `${slot_pointer_events}`,
          position: `${epostiontype}`,

          // "relative",
          top: `${etop}`,
          left: `${eleft}`

        }}

        onMouseUp={() => change_mouse_status(0)}
        onMouseDown={() => change_mouse_status(1)}

      >
        this is a list
      </div>
    );
  }

  function Slot(props) {
    return (
      <div id={props.slot_id} style={{



        display: "inline-flex", padding: "10px", margin: "10px", backgroundColor: "#e8e8ff", height: "200px", width: "200px"
      }}>
        {props.children}
this is slot (lsit container)
      </div>
    );
  }

  function Container(props) {
    return (
      <div id="Container_1"
        name='testname'
        style={{
          display: "inline-flex",
          backgroundColor: "#94e49d38",
          height: "400px",
          width: "100vw-10px"
        }}
      >


        {props.children}

      </div>
    );
  }
}```

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

从特定的第一个元素获取向量对的第二个元素

获取第二个系列中第一个系列的元素索引

使用fopen PHP获取第一个和第二个元素

获取数组的第一个和第二个元素

在Prolog中获取元组的第一个/第二个元素

语言c char *从第二个元素而不是第一个元素开始

XPath-仅第二个级别元素的第一个元素

在第一个元素前面显示第二个 <span> 元素

单击第一个元素,更改第二个元素的样式

从列表中的3个元组中获取第一个和第二个元素-Haskell

从SQL查询中获取第一个/第二个/第三个元素

xsl:选择找到元素的第一个位置,而不是第二个

在元素的第二个动画上调用Promise函数而不是第一个?

如何仅选择第二个li元素的第一个孩子

从第二个列表中减去第一个列表中的元素

如何用第二个序列的元素填充第一个序列

迭代向量对并访问第一个和第二个元素

如何重新排序元素,使第一个元素在右侧,第二个元素在左侧?

PHP合并元素,第一个元素相同,第二个元素相同

c ++-如果用作map中的键,如何获取对的第一个和第二个元素?

如何从此JSON数据的第二个元素中获取第一个值?

如何将数组元素作为子数组插入,例如第一个元素是第二个的父级,第二个是第三个的父级,依此类推?

访问第二个最小的第二个元素的排序嵌套列表的第一个元素

从数组1的第一个元素,数组2的第一个元素,数组1的第二个元素,数组2的第二个元素等创建python数组

如何使用 Java Streams 将第一个元素乘以第二个元素

Windbg 管理对象 poi 数组的第一个元素,第二个元素

将Class添加到第二个元素,从第一个元素开始计数

在Haskell中按第一个元素然后第二个元素对元组列表进行排序

元组列表列表,按第一个元素分组并添加第二个元素