简体   繁体   English

固定位置不是相对于IE11中的父容器

[英]fixed position is not relative to parent container in IE11

If we define a transform in the parent container the child position will inherit from the parent container in chrome and firefox but not in ie. 如果我们在父容器中定义一个转换,则子位置将从chrome和firefox中的父容器继承而来,而不是从ie中继承。

So what do I have to do for it to work in ie11 so it's always relative to the parent container? 那么,要使它在ie11中正常工作,我必须做什么呢?

 div.fixed { position: fixed; top: 100px; width: 300px; height: 100px; border: 3px solid #73AD21; background-color: red; transform: translate3d(0, 0px, 0); } div.fixed2 { position: fixed; top: 100px; width: 300px; height: 100px; border: 3px solid #73AD21; background-color: blue; } 
 <div class="fixed"> <div class='fixed2'></div> </div> 

The issue is that you have a transform value set on the first - this creates a new stacking context for position:fixed to be placed from - if you remove that then it will work properly. 问题是您在第一个上设置了transform值-这将为position:fixed放置一个新的堆叠上下文-如果将其删除,则它将正常工作。 If you need to force things onto the GPU for performance then nest the element that needs it. 如果您需要将某些东西强加到GPU上以提高性能,请嵌套需要它的元素。 Spec 规格

why do you need fixed element inside fixed element? 为什么在固定元素内需要固定元素?

you can simply use position: absolute for the child i think. 您可以简单地使用position: absolute我认为position: absolute适合孩子。 please check if this works for you. 请检查是否适合您。

if you really need both of them to be fixed, i think you can simple make 2 elements as siblings 如果您确实需要固定两个元素,我认为您可以简单地将2个元素作为同级元素

here is the updated snippet 这是更新的片段

 div.fixed { position: fixed; top: 100px; width: 300px; height: 100px; border: 3px solid #73AD21; background-color: red; transform: translate3d(0, 0px, 0); } div.fixed2 { position: absolute; top: 100px; width: 300px; height: 100px; border: 3px solid #73AD21; background-color: blue; } 
 <div class="fixed"> <div class='fixed2'></div> </div> 

Please read up on the position property. 请阅读position属性。 I suggest https://www.w3schools.com/cssref/pr_class_position.asp as a good start. 我建议将https://www.w3schools.com/cssref/pr_class_position.asp作为一个好的开始。

Also, as a matter of good programming practice please avoid using words like fixed as a name for a class as this may be reserved. 另外,根据良好的编程习惯,请避免使用诸如fixed类的单词作为类的名称,因为这可能是保留的。 Overwriting such can cause problems. 覆盖可能会导致问题。 Also, please use more descriptive names. 另外,请使用更多描述性名称。

If you want the inner div positioned relative to your outer div is suggest using code similar to the following... 如果你想在内部div定位相对于你的外div使用类似于下面的代码是建议...

 div.outerDiv { background-color : red; border : 3px solid #73AD21; height : 100px; position : fixed; top : 100px; transform : translate3d( 0, 0px, 0); width : 300px; } div.innerDiv { background-color : blue; border : 3px solid #73AD21; height : 100px; left : 20px; position : absolute; top : 10px; width : 300px; } 
 <div class = "outerDiv"> <div class = "innerDiv"> </div> </div> 

Fixed means that the element is positioned against the window, not the parent element. 固定表示元素是靠窗口放置的,而不是父元素。

Just add " left: 10px " and you'll see that it's not positioned against the parent. 只需添加“ left: 10px ”,您会发现它的位置与父级不对。 In all brosers. 在所有经纪人中。

https://jsfiddle.net/4agxb8z4/ https://jsfiddle.net/4agxb8z4/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM