Created
October 13, 2021 13:46
-
-
Save gchartier/61f4a6c47e7117840d94e173d522a4ad to your computer and use it in GitHub Desktop.
Fixed Element Transform Parent Finder
This file contains hidden or 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 this with a relevant selector. | |
| // If you use a tool that auto-generates classes, | |
| // you can temporarily add an ID and select it | |
| // with '#id'. | |
| 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 | |
| } = getComputedStyle(parent); | |
| if (transform !== 'none' || willChange === 'transform') { | |
| console.warn( | |
| '🚨 Found a culprit! 🚨\n', | |
| parent, | |
| { transform, willChange } | |
| ); | |
| } | |
| parent = parent.parentElement; | |
| } | |
| } | |
| findCulprits(document.querySelector(selector)); |
If using an iFrame, you'll have to select it in the browser settings for this snippet to work
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This snippet will crawl up the DOM tree checking each parent to see if it includes any properties that break position: fixed. There may be more than one culprit.
Once you find the element(s) in question, you can try removing the properties, or finding a non-transform alternative. If this isn't possible, you should consider moving the fixed element into a portal, as we discussed earlier.