I have a situation where, in normal CSS circumstances, a fixed div would be positioned exactly where it is specified (top:0px
, left:0px
).
This does not seem to be respected if I have a parent that has a translate3d transform. Am I not seeing something? I have tried other webkit-transform like style and transform origin options but had no luck.
I have attached a JSFiddle with an example where I would have expected the yellow box be at the top corner of the page rather than inside of the container element.
You can find below a simplified version of the fiddle:
<br>
<div style='position:relative; border: 1px solid #5511FF;
-webkit-transform:translate3d(0px, 20px , 0px);
height: 100px; width: 200px;'
>
<div style='position: fixed; top: 0px;
box-shadow: 3px 3px 3px #333;
height: 20px; left: 0px;'
>
Inner block
</div>
</div>
Any pointers to make the translate3d work with a fixed-positioned children will be appreciated.
As Bradoergo suggested, just get the window
scrollTop
and add it to the absolute position top like:This worked for me anyway.
So,
Not having found a satisfactory method to deal with this; and doing some off-canvas pane stuff (requiring translate) - I dug in a bit and discovered that (at least for my use-case) the best method to deal with this is to apply the same translate, but break children that need to be fixed out of their parent (translated) element; and then apply the translate to a div inside the
position: fixed
wrapper.The results look something like this (in your case):
fiddle fork: https://jsfiddle.net/hju4nws1/
While this may not be ideal for some use cases, typically if you're fixing a div you probably could care less about what element is its parent/where it falls in the inheritance tree in your DOM, and seems to solve most of the headache - while still allowing both
translate
andposition: fixed
to live in (relative) harmony.In Firefox and Safari you can use
position: sticky;
instead ofposition: fixed;
but it will not work in other browsers. For that you need javascript.I ran across the same problem. The only difference is that my element with 'position: fixed' had its 'top' and 'left' style properties set from JS. So I was able to apply a fix:
oRect object will contain real (relative to view port) top and left coordinates. So you can adjust your actual oElement.style.top and oElement.style.left properties.
One way to deal with this is to apply the same transform to the fixed element: