javascript - Get Z-index of each element on the page -


i trying find highest z-index on page. using this

var getstyleprop = function(el, prop){             return window.getcomputedstyle(el, null).getpropertyvalue(prop); } var gethighestzindex = function(){              var highestzindex = 0,                 htmlelems = ["a","abbr","acronym","address","applet","area","article","audio","b","base","basefont","bdi","bdo","big","blink","blockquote","body","br","button","canvas","caption","center","cite","code","col","colgroup","content","data","datalist","dd","decorator","del","details","dfn","dialog","dir","div","dl","dt","element","em","embed","fieldset","figcaption","figure","footer","form","frame","frameset","h1, h2, h3, h4, h5, h6","head","header","hgroup","hr","html","i","iframe","img","input","ins","isindex","kbd","keygen","label","legend","li","link","listing","main","map","mark","menu","menuitem","meta","meter","nav","noembed","noscript","object","ol","optgroup","option","output","p","param","plaintext","pre","progress","q","rp","rt","rtc","ruby","s","samp","script","section","select","shadow","small","source","spacer","span","strike","strong","style","sub","summary","sup","table","tbody","td","template","textarea","tfoot","th","thead","time","title","tr","track","tt","u","ul","var","video","wbr","xmp"],                 tags,                 zindex;              (var = 0; < htmlelems.length; i++){                  tags = document.getelementsbytagname(htmlelems[i]);                  if (tags){                     (var c = 0; c < tags.length; c++){                         zindex =getstyleprop(tags[c], "z-index");                            console.log(tags[c], "zindex=", zindex);                         if (zindex > highestzindex){                             highestzindex = zindex;                         }                      }                    }               }              return highestzindex;          } console.log(gethighestzindex()); 

but coming "auto". this ancient article explains how "bug" causing behavior. i've tried make clones of each node, set position relative, , z-index,

cl.style.display = "none"; cl.style.position = "absolute"; zindex = (getstyleprop(cl, "z-index")); 

but not working either. wrong here? there cheaper way recreating on page?

jsbin

the node's clone not seem z-index, while node returns right value. try using instead (not sure how might react on page lots of content):

var gethighestzindex = function () {     var highestzindex = 0,         zindex,         pos,         tags = document.body.getelementsbytagname('*');      (var c = 0; c < tags.length; c++) {         // original 'position' property         pos = getcomputedstyle(tags[c]).position;         // set temporarily 'relative'         tags[c].style.position = "relative";         // grab z-index         zindex = getcomputedstyle(tags[c]).zindex;         // reset 'position'         tags[c].style.position = pos;          console.log(tags[c], "zindex=", zindex);          if (zindex > highestzindex) { highestzindex = zindex; }     }      return highestzindex; };  console.log(gethighestzindex()); 

js fiddle demo

changing element's position temporarily might produce glitch. you'll need test on page lots of contents , elements position:absolute; or position:fixed;.


Comments

Popular posts from this blog

java - Could not locate OpenAL library -

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

sorting - opencl Bitonic sort with 64 bits keys -