public
Created

Returns all elements under the position [clientX,clientY] in an array

  • Download Gist
gistfile1.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
/**
* Returns all elements under the position [clientX,clientY] in an array
*
* @param int X position relative to the viewport
* @param int Y position relative to the viewport
* @param domElm The root element at which to stop looping. Defaults to document.body
*
* @returns array List of elements under the given position
*/
document.elementsFromPoint = function(clientX,clientY,root) {
// root defaults to document.body
if (root == undefined)
root = document.body;
var elements = [];
var elm;
while (elm = document.elementFromPoint(clientX,clientY)) {
if (elm == root)
break;
elements.push(elm);
// store the original display style and hide the current layer by setting display to none
elm.oldDisplay = elm.style.display;
elm.style.display = 'none';
}
// restore the original display values
for (var i = 0; i < elements.length; i++) {
elements[i].style.display = elements[i].oldDisplay;
delete elements[i].oldDisplay;
}
 
return elements;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.