Skip to content

Instantly share code, notes, and snippets.

@gwwar
Last active July 15, 2016 09:30
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gwwar/2f661deec7b99a1a418b to your computer and use it in GitHub Desktop.
Save gwwar/2f661deec7b99a1a418b to your computer and use it in GitHub Desktop.
Given a node, returns the closest stacking context.
function getClosestStackingContext( node ) {
if( ! node || node.nodeName === 'HTML' ) {
console.log( node, 'has stacking context, reason: root' );
return document.documentElement;
}
const computedStyle = getComputedStyle( node );
if ( computedStyle.position === 'fixed' ) {
console.log( node, 'has stacking context, reason: position: fixed' );
return node;
} else if ( computedStyle.zIndex !== 'auto' && computedStyle.position !== 'static' ) {
console.log( node, `has stacking context, reason: zindex: ${ computedStyle.zIndex } and position: ${ computedStyle.position }` );
return node;
} else if ( computedStyle.opacity !== '1' ) {
console.log( node, `has stacking context, reason: opacity: ${ computedStyle.opacity }` );
return node;
} else if ( computedStyle.transform !== 'none' ) {
console.log( node, `has stacking context reason: transform: ${ computedStyle.transform }` );
return node;
} else if ( computedStyle.mixBlendMode !== 'normal' ) {
console.log( node, `has stacking context, reason: mix-blend-mode: ${ computedStyle.mixBlendMode }`);
return node;
} else if ( computedStyle.filter !== 'none' ) {
console.log( node, `has stacking context, reason: filter: ${ computedStyle.filter }` );
return node;
} else if ( computedStyle.isolation === 'isolate' ) {
console.log( node, 'has stacking context, reason: isolation: isolate' );
return node;
} else if ( computedStyle.webkitOverflowScrolling === 'touch' ) {
console.log( node, 'has stacking context, reason: -webkit-overflow-scrolling: touch' );
return node;
} else {
return getClosestStackingContext( node.parentNode );
}
}
@gwwar
Copy link
Author

gwwar commented Dec 10, 2015

const node = document.querySelector( '.my-component' );
getClosestStackingContext( node ); //should return itself if it has created a stacking context
getClosestStackingContext( node.parentNode ); //returns the parent stacking context

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