Blog Has Moved
Tuesday, July 28, 2009
Since migrating my site to Drupal I now no longer use Blogger and hence my blog has moved here.
Since migrating my site to Drupal I now no longer use Blogger and hence my blog has moved here.
Flash developers have the pleasure of having a nice little built-in method called getNextHighestDepth. Which returns the next highest layer number for any object they call it on. Which is very useful to make sure that what you want to be on top is indeed on top.
function getNextHighestZindex(obj){
var highestIndex = 0;
var currentIndex = 0;
var elArray = Array();
if(obj){ elArray = obj.getElementsByTagName('*'); }else{ elArray = document.getElementsByTagName('*'); }
for(var i=0; i < elArray.length; i++){
if (elArray[i].currentStyle){
currentIndex = parseFloat(elArray[i].currentStyle['zIndex']);
}else if(window.getComputedStyle){
currentIndex = parseFloat(document.defaultView.getComputedStyle(elArray[i],null).getPropertyValue('z-index'));
}
if(!isNaN(currentIndex) && currentIndex > highestIndex){ highestIndex = currentIndex; }
}
return(highestIndex+1);
}
So far I have test it on Firefox, IE6, IE7, Opera, and Safari in XP. Also I have tested it on Firefox and Safari on OSX. It works perfectly in all but Safari on OSX (10.4). So if you are are a javascript developer and know how to adjust this function to work on Safari on a Mac please leave a comment :)Labels: javascript, z-index
5 Comments:
Great! I need it.
thank u!
This is a fantastic piece of script!
I know this isn't the comment you were looking for above, but thanks heaps for the function.
I've used the Actionscript one infinitum and it's excellent to finally find a Javascript alternative!
Cheers.
Oh God. Thanks for this script. I adapted it to run in the Script# (ScriptSharp) framework and it works great.
One thing I had to add is a check for the maximum value of the z-index property. In IE6 setting a z-index higher than 2147483647 results in an element with a z-index of 2147483647. The script was giving me this number and when I set my elements to this value, they got messed up.
I wrote: if(!isNaN(currentIndex) && currentIndex < 16777271 && currentIndex > highestIndex)
Where 16777271 is the maximum value for Safari 3. See here for more details: http://www.puidokas.com/max-z-index/
thanks for the contribution dru!
Thanks man . It helped me a lot.
Thumbs UP.
Post a Comment
<< Home