OK guys, here's a tricky one (perhaps): is there any to get a sort of "global z-index" of an element? here is an example of what i'd like to do:
given two element references, how would you determine which one is hiher in the z order (given that they may not have assigned z-indices or are in separated containers)
Elaborate hacks welcome! Not-so elaborate methods more than welcome.
This was an interesting problem. The following code may not be bug free. I just put it together right now and haven't tested it entirely. Its purpose is to demonstrate an approach.
It compares two elements and tests them on three criteria:
Looking through the ancestor tree of each element, if the first ancestor for element A to have a z-index has a greater z-index than the first ancestor of element B to have a z-index (if one exists for element B at all), then element A is higher in the z-order.
Otherwise, if element B is a descendent of element A then element B has a higher z-order.
Otherwise, looking at the siblings of the last ancestor that element A and element B had in common, if an ancestor of element A comes first in the array of children of that common ancestor then element B has a higher z order.
There may be an simpler way of describing of the logic above and the algorithm can be improved. (For example, the second check above could be done first.) However, here's the code:
Play around with the nested
div
s in the body to test it working.