Skip to content

Instantly share code, notes, and snippets.

@jsatk
Last active December 31, 2015 02:29
Show Gist options
  • Save jsatk/7921133 to your computer and use it in GitHub Desktop.
Save jsatk/7921133 to your computer and use it in GitHub Desktop.
For when you really want your shit on top.
var fuckYouIWantMyShitOnTop = function (idOfElementYouWantOnTop) {
var all = document.getElementsByTagName("*"),
maxZIndex = 9007199254740992, // https://gist.github.com/jsatk/7921133#comment-968150
i, highestZIndex, currentZIndex, currentEl;
for (i = all.length - 1; i >= 0; i--) {
currentEl = all[i];
currentZIndex = Number(currentEl.style.zIndex);
if (isNaN(currentZIndex)) continue;
highestZIndex = isNaN(highestZIndex) || currentZIndex > highestZIndex ? currentZIndex : highestZIndex;
currentEl.style.zIndex = currentZIndex === maxZIndex ? currentZIndex - 1 : currentZIndex;
}
document.getElementById(idOfElementYouWantOnTop).style.zIndex = highestZIndex + 1;
};
@msepcot
Copy link

msepcot commented Dec 12, 2013

Page 29 of the ECMAScript (under ECMA Section 8.5 - Numbers) spec states:

Note that all the positive and negative integers whose magnitude is no greater than 253 are representable in the Number type (indeed, the integer 0 has two representations, +0 and −0).

So,

var fuckYouIWantMyShitOnTop = function (yourElementYouWantOnTop) {
  yourElementYouWantOnTop.style.zIndex = 9007199254740992;
}

@jsatk
Copy link
Author

jsatk commented Dec 12, 2013

OK now that's just rude.

@chriscoyier
Copy link

Another way maybe to do it would be to wrap the entire contents of the body in a <div> with position: relative; z-index: 1; and like overflow: auto; then insert your thing AFTER that div with also some positioning other than static and a z-index: 2; - That way everything else is stuck in that new context and your stuff is in a new, higher context. I think that should work.

@joelmccracken
Copy link

You might want to lower the zindex of everything else that is at 9007199254740992.

@jsatk
Copy link
Author

jsatk commented Dec 12, 2013

@joelmccracken @msepcot Done :)

@chriscoyier that's brilliant and terrifying. This entire thing is terrifying and was created as a joke. The only real use case I can see for this is if you're on a giant project and some element keeps getting covered up by other non-careful coders who abuse z-index you could invoke this script with the ID if the el you want on top somewhere ;)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment