Created
June 24, 2013 12:43
-
-
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.
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
/* | |
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