Last active
April 14, 2021 09:17
-
-
Save alenaksu/006b4d96abfffe9d9e9f646147abf632 to your computer and use it in GitHub Desktop.
Document.elementsFromPoint polyfill
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
if (!document.elementsFromPoint) { | |
document.elementsFromPoint = | |
document.msElementsFromPoint || | |
function elementsFromPoint(x, y) { | |
var stack = []; | |
var element = document.elementFromPoint(x, y); | |
try { | |
while (element !== null) { | |
stack.push({ | |
element: element, | |
value: element.style.getPropertyValue('pointer-events'), | |
priority: element.style.getPropertyPriority('pointer-events') | |
}); | |
/** | |
* [...]Note: The elementFromPoint() method does not necessarily return the top-most painted element. | |
* For instance, an element can be excluded from being a target for hit testing by using the pointer-events | |
* CSS property.[...] | |
* https://drafts.csswg.org/cssom-view/#dom-document-elementfrompoint | |
*/ | |
element.style.setProperty('pointer-events', 'none', 'important'); | |
element = element !== document.documentElement | |
? document.elementFromPoint(x, y) | |
: null; | |
} | |
} catch (e) {} | |
return stack.map(function (entry) { | |
// restore its previous value if any | |
entry.element.style.setProperty('pointer-events', entry.value, entry.priority); | |
return entry.element; | |
}); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment