Last active
June 11, 2021 07:10
-
-
Save iddan/54d5d9e58311b0495a91bf06de661380 to your computer and use it in GitHub Desktop.
document.elementsFromPoint Polyfill
This file contains 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
'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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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: