Skip to content

Instantly share code, notes, and snippets.

@oslego
Created June 24, 2013 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oslego/5849786 to your computer and use it in GitHub Desktop.
Save oslego/5849786 to your computer and use it in GitHub Desktop.
Forcefully stack an element on top by assigning it the highest z-Index possible.
/*
Make sure the input element is the highest one in the page by z-index
@param {DOM Element} el Element to stack at the top
@param {DOM Element} ref BoundingClientRect reference of where to compare topl-left corner
*/
function stackOnTop(el, ref){
// elementFromPoint returns null if element is outside visible viewport
// scroll the page so the element comes into view
if (window.innerHeight < ref.top){
window.scrollTo(0, ref.top)
}
var hit = document.elementFromPoint(ref.left, ref.top - window.pageYOffset)
while(!hit.isSameNode(el)){
var zIndex = el.style.zIndex * 10
console.info('again with z-index %d', zIndex)
// see http://softwareas.com/whats-the-maximum-z-index
if (zIndex > 2147483647){
console.warn('screw this, I give up at z-index: ', zIndex)
return
}
el.style.zIndex = zIndex
stackOnTop(el, ref)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment