Why does document.elementFromPoint(500,1000)
here return null if that pixel is located outside the visible document when the document loads?
I've noticed document.elementFromPoint
returns null for any point that is initially outside the visible document, as well as after it is scrolled into view.
A simple way to test this is in Chrome (ctrl-shift-i -> scripts -> 'watch expressions') (ensure that the page height is narrowed to less than 1000 pixels)
EDIT: so it does make sense, as per docs
I failed on both assumptions,
So you kinda answered your own question: document.elementFromPoint works in the viewport coordinate rather than document. So all you need to do is to add a scroll compensation.
The following code worked out for me:
document.elementFromPoint(X - window.pageXOffset, Y - window.pageYOffset);
Or if you are listening for an event that would be:
document.elementFromPoint(e.pageX - window.pageXOffset, e.pageY - window.pageYOffset);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With