Skip to content

Instantly share code, notes, and snippets.

@duzun
Created November 12, 2014 09:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save duzun/9e6dc3d6e8c436d4172e to your computer and use it in GitHub Desktop.
Save duzun/9e6dc3d6e8c436d4172e to your computer and use it in GitHub Desktop.
Finds maximum zIndex of an element
/**
* Finds maximum zIndex of an element
*/
;(function (global) {
var $ = global.jQuery || global.Zepto;
var maxZIndex = function maxZIndex(el,min) {
var el = this
, zIdx = el.css('zIndex')
, cpr = zIdx === 'auto' ? 0 : zIdx
, _cnt = 0
, _lev = 0
, $e = $([0])
, rec_cpr = function rec_cpr(v,e) {
$e[0] = e;
++_cnt;
if($e.css('position') == 'static') {
++_lev;
$e.children().each(rec_cpr);
--_lev;
}
else {
v = parseInt($e.css('zIndex'));
if(!isNaN(v) && cpr <= v) {
++v;
zIdx = cpr = v;
}
}
}
;
if(min != NULL) {
zIdx = cpr = Math.max(min, cpr);
}
el.siblings().each(rec_cpr);
return zIdx
};
$.fn.maxZIndex = maxZIndex;
})(this);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment