I would like to make an element (e.g. a <div>
) be the top-most layer on the page.
My assumption is that the only way I can do this is to specify that the element has a style="z-index:"
value that is the maximum the browser allows (int32?).
Is this correct?
Instead, would it be possible to somehow get the element's z-index
whose is highest, and make this <div>
's z-index
the [highest element's value] + 1
? For example:
$myDiv.css("z-index", $(document.body).highestZIndex() + 1);
How do modal JavaScript "windows" work?
Here's how to do it :
highest_index now contains the highest z-index on the page... just add 1 to that value and apply it wherever you want. You can apply it like so :
Here's another way of achieving the same thing using jQuery :
Again, same way to apply the new index to an element :
http://abcoder.com/javascript/a-better-process-to-find-maximum-z-index-within-a-page/ -> find the max z-index and assign +1 to it.
What about stacking context? It is not always true that: On a document highest z-index will be on top. See: http://philipwalton.com/articles/what-no-one-told-you-about-z-index/. If you do not take stacking context into account, setting a billion may not be enough to make your element on the top-most.
Sheavi's jQuery solution doesn't work because z-index is a css style, not an attribute.
Try this instead:
Return value may not be what you expect due to Javascript's async nature, but calling the function on any element will work fine.