This polyfill comes to solve missing elementsFromPoint in some browsers / specific versions, but to use msElementsFromPoint if exists.
Using VanillaJS, very small package size.
- Check for native
document.elementsFromPoint
- If doesn't exist, try fallback to
document.msElementsFromPoint
- Otherwise, use
elementsFromPoint
polyfill function.
Polyfill function is based on https://gist.github.com/iddan/54d5d9e58311b0495a91bf06de661380
First install package:
$ npm install --save polyfill-elements-from-point
On application load:
import elementsFromPointPolyfill from 'polyfill-elements-from-point'
elementsFromPointPolyfill(window)
And then window.document.elementsFromPoint would be available no matter which browser you're using :)
- Keep in mind some browsers / some versions are returning
null
. See documantation.
Although the polyfill function will always return an array, I didn't want to wrap whole function to return[]
if receivednull
, but to return original value from native functions. - By using babel preset env, I'm assuring browsers compatibility.