Skip to content

Instantly share code, notes, and snippets.

@MattyQ
Last active April 26, 2019 17:17
Show Gist options
  • Save MattyQ/54465699ccce526a24edea60ee50e9c7 to your computer and use it in GitHub Desktop.
Save MattyQ/54465699ccce526a24edea60ee50e9c7 to your computer and use it in GitHub Desktop.
Get highest z-index in a given target (Javascript)
/**
* @fileoverview Queries the computed styles for a given element to determine the highest z-index.
* For example, GET_Z_INDEX(document) returns the highest z-index in the DOM.
*/
/**
* @function GET_Z_INDEX
* @param {object} parent_node The target parent node. The function returns the highest z-index of parent_node's children.
* @returns {number}
*/
function GET_Z_INDEX(parent_node) {
var stored_z_index = 0;
parent_node.querySelectorAll("*").forEach(function(element) {
var z_index = parseInt(getComputedStyle(element).zIndex);
if (isNaN(z_index) === false && z_index > stored_z_index) {
stored_z_index = z_index;
}
});
return stored_z_index;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment