Skip to content

Instantly share code, notes, and snippets.

@kaimallea
Created August 19, 2011 21:42
Show Gist options
  • Save kaimallea/1158088 to your computer and use it in GitHub Desktop.
Save kaimallea/1158088 to your computer and use it in GitHub Desktop.
Return the highest z-Index used across all elements
/**
* Return the highest z-Index used across all elements
**/
function getHighIndex (selector) {
// No granularity by default; look at everything
if (!selector) { selector = '*' };
var elements = document.querySelectorAll(selector) ||
oXmlDom.documentElement.selectNodes(selector),
i = 0,
e, s,
max = elements.length,
found = [];
for (; i < max; i += 1) {
e = window.getComputedStyle(elements[i], null).zIndex || elements[i].currentStyle.zIndex;
s = window.getComputedStyle(elements[i], null).position || elements[i].currentStyle.position;
// Statically positioned elements are not affected by zIndex
if (e && s !== "static") {
found.push(parseInt(e, 10));
}
}
return found.length ? Math.max.apply(null, found) : 0;
}
@rudiedirkx
Copy link

You could write:

if (!selector) { selector = '*' };

more modernly as:

selector || (selector = '*');

Not better. Not worse. Cooler.

@kaimallea
Copy link
Author

Totally missed this comment! I chose the former for clarity, but yes there is a cooler way. ;-)

@asuraphel
Copy link

Not sure why but it gives me NaN on Firefox's console.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment