Created
April 20, 2016 18:03
-
-
Save monochromer/4b5d8c200b45af4b15b9a55fb1c7daf4 to your computer and use it in GitHub Desktop.
closest 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
;(function(Element) { | |
// matches polyfill | |
if (!Element.prototype.matches) { | |
Element.prototype.matches = Element.prototype.matchesSelector || | |
Element.prototype.webkitMatchesSelector || | |
Element.prototype.mozMatchesSelector || | |
Element.prototype.msMatchesSelector || | |
function matches(selector) { | |
var element = this, | |
elements = (element.document || element.ownerDocument).querySelectorAll(selector), | |
index = 0; | |
while (elements[index] && elements[index] !== element) { | |
++index; | |
} | |
return elements[index] ? true : false; | |
}; | |
}; | |
//closest polyfill | |
if (!Element.prototype.closest) { | |
Element.prototype.closest = function(selector) { | |
var node = this; | |
while (node) { | |
if (node.matches(selector)) return node; | |
else node = node.parentElement; | |
} | |
return null; | |
}; | |
} | |
})(Element); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment