How to find the highest z index in a document no matter what tags they are?

How can I find the highest z index in a document no matter what tags they are?

I found this code, but I tested it and it doesn’t work,

//include jQuery.js -- visit: http://jquery.com/ $(function(){ var maxZ = Math.max.apply(null,$.map($('body > *'), function(e,n){ if($(e).css('position')=='absolute') return parseInt($(e).css('z-index'))||1 ; }) ); alert(maxZ); }); 

Is there a better way to do this?

EDIT:

It seems that if I change the code to this,

 $('body *') $('body > *') --> is for div only I guess. 
+6
source share
2 answers

This is not the most effective solution, but it should work. jsFiddle .

Note that you must specify the position specified so that z-index returns a value.

 var highest = -999; $("*").each(function() { var current = parseInt($(this).css("z-index"), 10); if(current && highest < current) highest = current; }); alert(highest); 
+6
source

It works:

 var maxZ=0; $('*').each(function(){ if($(this).css('zIndex') > maxZ) maxZ = $(this).css('zIndex'); }) console.log(maxZ); 

JsFiddle example

I think one of the problems with the code you posted is that you only check for absolutely positioned elements.

+3
source

Source: https://habr.com/ru/post/949597/


All Articles