Skip to content

Instantly share code, notes, and snippets.

@linj121
Created May 14, 2024 02:05
Show Gist options
  • Save linj121/b0bc7b35627973a1a75d5e4e719c3214 to your computer and use it in GitHub Desktop.
Save linj121/b0bc7b35627973a1a75d5e4e719c3214 to your computer and use it in GitHub Desktop.
// 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