Skip to content

Instantly share code, notes, and snippets.

@iddan
Last active June 11, 2021 07:10
Show Gist options
  • Save iddan/54d5d9e58311b0495a91bf06de661380 to your computer and use it in GitHub Desktop.
Save iddan/54d5d9e58311b0495a91bf06de661380 to your computer and use it in GitHub Desktop.
document.elementsFromPoint Polyfill
'use strict';
if (!document.elementsFromPoint) {
document.elementsFromPoint = elementsFromPoint;
}
function elementsFromPoint(x, y) {
var parents = [];
var parent = void 0;
do {
if (parent !== document.elementFromPoint(x, y)) {
parent = document.elementFromPoint(x, y);
parents.push(parent);
parent.style.pointerEvents = 'none';
} else {
parent = false;
}
} while (parent);
parents.forEach(function (parent) {
return parent.style.pointerEvents = 'all';
});
return parents;
}
@ddrscott
Copy link

ddrscott commented May 1, 2017

Thanks! Works for me.

@tnicola
Copy link

tnicola commented Mar 16, 2019

Thanks! It works great but I've changed a bit the if condition. If document.elementFromPoint(x, y) returns null it would raise an error since at this point parent.style.pointerEvents the parent would be null.

I've updated my code with:

...
const element = document.elementFromPoint(x, y);
if (element && parent !== element) {
            parent = element;
            parents.push(parent);
            parent.style.pointerEvents = 'none';
} else {
...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment