I have a scrolling element with a fixed background:
.my-element {
background: url(/foo.png) no-repeat right bottom;
background-attachment: fixed;
}
It works great! Normally. However, if I apply a translate transform (even of 0) to it (which I need for an animation), the background shifts to be non-fixed (it anchors to the bottom of .my-element, which is scrolled out of view:
.my-element {
background: url(/foo.png) no-repeat right bottom;
background-attachment: fixed;
transform: translateX(0); // this breaks the "fixed" behavior
}
There was a similar question about this here, but the answers from 4 years ago say the bug has been fixed, and that it was only firefox. This is happening to me in Firefox and Chrome.
Is there a way I can avoid this? Or is there a way to get the same behavior w/o the background-attachment property? Thanks!
edit: just added a jsfiddle demonstrating the issue: https://jsfiddle.net/mozges0k/7/
I had a pretty hard time finding a workaround. Extracting the background image to its own container and adding
position: stickyto it seemed like a viable solution, but it didn't work out in the end.There is one more solution that came to my mind: What if you extract
transformto its own class and remove that class when the animation has finished? It seems likebackground-attachmentstarts working again whentransformis removed, even at runtime. Then thebackground-attachmentcan do its magic and your animation can run too.While its not as easy and direct as I would like it to be, this would be a solution. Let me know what you think, and good luck!