Created
May 14, 2021 17:31
-
-
Save tanthammar/af0c372f05058daa96b662185b5f2f40 to your computer and use it in GitHub Desktop.
JS Get highest z-index, always on top
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
window.findHighestZ = () => [...document.querySelectorAll('body *')] | |
.map(elt => parseFloat(getComputedStyle(elt).zIndex)) | |
.reduce((z, highest=Number.MIN_SAFE_INTEGER) => | |
isNaN(z) || z < highest ? (isNaN(highest) ? 1 : highest) : z | |
) | |
//vanilla | |
document.getElementById("foo").style.zIndex = window.findHighestZ() | |
//alpine | |
this.$refs.foo.style.zIndex = window.findHighestZ() |
Recommend to add style="isolation: isolate"
to the outmost container. (TailwindCSS class="isolate"
)
https://www.joshwcomeau.com/css/stacking-contexts/#creating-stacking-contexts
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use it for menus, layering multiple modals, notifications etc