Created
November 12, 2014 09:32
-
-
Save duzun/9e6dc3d6e8c436d4172e to your computer and use it in GitHub Desktop.
Finds maximum zIndex of an element
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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