Last active
November 20, 2017 05:25
-
-
Save nuxodin/6d937dddf7e0deacfef484feca11a147 to your computer and use it in GitHub Desktop.
Browsers have "elementFromPoint", here is "nodeFromPoint" (including text-nodes) !
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Copyright (c) 2016 Tobias Buschor https://goo.gl/gl0mbf | MIT License https://goo.gl/HgajeK */ | |
document.nodeFromPoint = function(x, y) { | |
const el = document.elementFromPoint(x, y); | |
const nodes = el.childNodes; | |
for (let i = 0, node; node = nodes[i++];) { | |
if (node.nodeType === 3) { | |
const range = document.createRange(); | |
range.selectNode(node); | |
const rects = range.getClientRects(); | |
for (let j = 0, rect; rect = rects[j++];) { | |
if (x > rect.left && x < rect.right && y > rect.top && y < rect.bottom) | |
return node; | |
} | |
} | |
} | |
return el; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment