Created
May 14, 2024 02:05
-
-
Save linj121/b0bc7b35627973a1a75d5e4e719c3214 to your computer and use it in GitHub Desktop.
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
// Author: Josh Comeau | |
// https://courses.joshwcomeau.com/css-for-js/ | |
// Helps developers finding the parent element that break the fixed position | |
// 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)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment