I
I
IDONTSUDO2020-04-23 14:10:45
JavaScript
IDONTSUDO, 2020-04-23 14:10:45

Document.elementFromPoint how to find all elements that occupy a coordinate?

I have such a thing.
5ea17208613c8023694082.png

Green is the shadow of the block being dragged with the mouse.
I have something like this function, in order to find the DIV from which you need to build this shadow.

document.querySelector('body').addEventListener(
  'mousemove',
  throttle(function(e) {
          let Shadow = document.elementFromPoint(e.x - 2, e.y - 2);
         }


But the problem is that when I move the mouse to the shadow itself. I'm getting the element with that shadow, not the DIV below it. If there is a way, get the div under it?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
IDONTSUDO, 2020-04-23
@IDONTSUDO

Found on the Internet.

function getAllElementsFromPoint(x, y) {
    var elements = [];
    var display = [];
    var item = document.elementFromPoint(x, y);
    while (item && item !== document.body && item !== window && item !== document && item !== document.documentElement) {
        elements.push(item);
        display.push(item.style.display);
        item.style.display = "none";
        item = document.elementFromPoint(x, y);
    }
    // restore display property
    for (var i = 0; i < elements.length; i++) {
        elements[i].style.display = display[i];
    }
    return elements;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question