elementsFromPoint Returns The Hit Stack
elementsFromPoint returns every element under a coordinate from topmost down, so drop targets and hit tests can see past overlays that elementFromPoint hides.
Code
// elementsFromPoint 返回该坐标下从最顶到底的整条命中栈,
// elementFromPoint 只给最上层一个
const target = document.createElement('div');
target.id = 'drop-zone';
target.style.cssText = 'position:fixed;top:40px;left:40px;width:120px;height:80px;';
document.body.append(target);
function topmostAt(x, y) {
return document.elementFromPoint(x, y);
}
function hitStackAt(x, y) {
// 遮罩挡在最上层时,复数形式能穿透拿到"指针下面还有什么"
return document.elementsFromPoint(x, y);
}
console.log('topmost:', topmostAt(100, 100)?.id ?? 'none');
console.log('stack:', hitStackAt(100, 100).map((el) => el.tagName.toLowerCase()));
// 坐标在视口外:单数返回 null,复数返回空数组
console.log('outside:', topmostAt(-10, -10), hitStackAt(-10, -10));elementsFromPoint Returns The Hit Stack: what runs and why
Everything happens inside the functions topmostAt and hitStackAt.
Caveats
- It is dependency-free in the strict sense — the import list, if any, is empty of packages.
- This is browser-side code —
documentwill not resolve in Node.
Using it
The definition you will edit: function topmostAt(x, y)
Output lines are prefixed (topmost:, stack:, outside:), so each prefix identifies the step it came from.
Key statements
return document.elementsFromPoint(x, y);
return document.elementFromPoint(x, y);
Interactive Tools for This Topic
A/B Test Sample Size CalculatorCalculate required per-variant sample size and experiment duration for two-proportion A/B tests from baseline conversion, MDE, alpha, and statistical power.DOM Event Propagation VisualizerVisualize DOM event capturing, targeting, and bubbling phases in real time. Toggle stopPropagation and stopImmediatePropagation to see how events traverse the tree.JSONPath TesterTest JSONPath expressions against JSON documents with dot notation, brackets, wildcards, and recursive descent.
FAQ
- Does elementsFromPoint Returns The Hit Stack run in plain Node.js?
- No — it touches `document`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.
Report a problem with this exampleOpens a pre-filled issue on GitHub.