Skip to content

Instantly share code, notes, and snippets.

@ststeiger
Forked from osartun/elementsFromPoint
Last active August 29, 2015 14:15
Show Gist options
  • Save ststeiger/5d9af0cc4d9a953b66f5 to your computer and use it in GitHub Desktop.
Save ststeiger/5d9af0cc4d9a953b66f5 to your computer and use it in GitHub Desktop.
(function ($, document, undefined) {
$.extend({
/**
* A static jQuery-method.
* @param {number} x The x-coordinate of the Point.
* @param {number} y The y-coordinate of the Point.
* @param {Element} until (optional) The element at which traversing should stop. Default is document.body
* @return {jQuery} A set of all elements visible at the given point.
*/
elementsFromPoint: function(x,y, until) {
until || (until = document.body); // Stop traversing here
if (until instanceof $) until = until[0];
var elements = [], previousDisplay = [], current, i, d, computedStyle = window.getComputedStyle;
while ( (current = document.elementFromPoint(x,y)) && current != null && current !== until) {
elements.push(current);
previousDisplay.push(computedStyle ? computedStyle(current)["display"] : current.currentStyle.display);
current.style.display = "none"; // Add display: none, to get to the underlying element
}
i = previousDisplay.length;
while ((d = previousDisplay.pop()) != null && i--) {
elements.get(i).style.display = d; // Restore the previous display-value
}
return $(elements);
}
});
})(jQuery, document);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment