Last active
July 10, 2023 11:32
-
-
Save bdsqqq/af4130e5618130a68f77ee70e482a1b4 to your computer and use it in GitHub Desktop.
Find who's fucking with fixed positioning (creating a stacking context) Thanks Josh!
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
// Replace “.the-fixed-child” for a CSS selector | |
// that matches the fixed-position element: | |
const selector = '.the-fixed-child'; | |
function findCulprits(elem) { | |
if (!elem) { | |
throw new Error( | |
'Could not find element with that selector' | |
); | |
} | |
let parent = elem.parentElement; | |
while (parent) { | |
const { | |
transform, | |
willChange, | |
filter, | |
} = getComputedStyle(parent); | |
if ( | |
transform !== 'none' || | |
willChange === 'transform' || | |
filter !== 'none' | |
) { | |
console.warn( | |
'🚨 Found a culprit! 🚨\n', | |
parent, | |
{ transform, willChange, filter } | |
); | |
} | |
parent = parent.parentElement; | |
} | |
} | |
findCulprits(document.querySelector(selector)); | |
/* | |
So how do we fix it, once we've found the culprit? | |
- If the culprit uses overflow: hidden, we can switch to overflow: clip. Because overflow: clip doesn't create a scroll container, it doesn't have this problem! | |
- If the culprit uses auto or scroll, you might be able to remove this property, or push it lower in the DOM. This is a tricky problem, often without a quick solution. | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
from joshwcomeau