简体   繁体   English

旋转后保持z索引顺序

[英]Maintaining z-index order after rotated

This is my div structure 这是我的div结构

<div id="outer">
   <div class="inner">
      <div>
         <div class="item"></div>   
      </div>
        <div class="itemBelow">
            <div class="innerBelow"></div>
        </div> 
   </div>
</div>

#outer{ /*parent and children - rotateable*/
  top:10px;
  width:220px;
  height:50px;
  position: absolute;
  background:blue;
}
.inner{
  top:35px;
  width:200px;
  height:75px;
  position: relative;
  background:green;
}
.item{
  top:60px;
  width:180px;
  height:100px;
  position: absolute;
  z-index: 4; /*to be top most div*/
  display: block;
  background:red;
}
.itemBelow{
  top:160px;
  width:160px;
  height:120px;
  position: relative;
  background:pink;
}
.innerBelow{
  top: 105px;
  width:140px;
  height:140px;
  position: relative;
  display: block;
  z-index: 1; /*to be lowest div*/
  background:lime;
}

I'm trying to rotate #outer div and its children. 我正在尝试旋转#outer div及其子级。

$('#btn').click(function(){
    $('.outer').css({
        '-webkit-transform': 'rotateZ(45deg)',
        '-msTransform': 'rotateZ(45deg)',
        'transform': 'rotateZ(45deg)',
    });
}); 

But how can I maintain the z-index order after rotation? 但是旋转后如何保持z索引顺序? I expect the output to remain in the below z-index order before and after #outer is rotated. 我希望在#outer旋转前后,输出将保持在以下z-index顺序。

item //top div
stillObj //second highest div
innerBelow //lowest div

I have created a mockup. 我创建了一个样机。 See Fiddle 小提琴

I have googled and come across many post like z-index and transform , transform-translate3d . 我已经用歌搜索,并遇到了很多帖子,例如z-index和transformtransform-translate3d But I'm unable to make this work. 但是我无法完成这项工作。

You can use the css property transform-style to keep your 'stacking order' or z-index when using css3 3d transforms. 使用css3 3d转换时,可以使用css属性的transform-style来保持“堆叠顺序”或z-index

You can see in this fiddle , even when transformed, the element ( .item ) retained it's z-index value relative to the .stillObj . 您可以在此小提琴中看到,即使进行了转换,元素( .item )仍保留了相对于.stillObjz-index值。

You have to apply transform-style to both elements that need to retain their z-index and will also be 'interacting' with a 3d transform . 您必须将transform-style应用于需要保留其z-index 两个元素,并且还需要与3d transform进行“交互”。

Here is an example of some of the types of structuring/class changes you'd have to make to your specific example in orderto keep the z-index values sticking: Fiddle . 这是为了使z-index值保持不变而必须对特定示例进行的某些结构/类更改类型的示例: Fiddle

It seems as if you need to put preserve-3d on the elements themselves (that will have a 3d transform ), not their parents in order to preserve the stacking order. 似乎您需要在元素本身(将进行3d transform )上而不是它们的父元素上preserve-3d以便保留堆叠顺序。

 $('#btn').click(function(){ $('.item').toggleClass('rotate'); }); 
 .stillObj{ height: 450px; width: 30px; background: #666; z-index:2;/*to be second height div*/ position: relative; } #outer{ /*parent and children - rotateable*/ top:10px; width:220px; height:50px; position: absolute; background:blue; } .inner{ top:35px; width:200px; height:75px; position: relative; background:green; } .item{ top:60px; width:180px; height:100px; position: absolute; z-index: 4; /*to be top most div*/ display: block; background:red; } .itemBelow{ top:160px; width:160px; height:120px; position: relative; background:pink; } .innerBelow{ top: 105px; width:140px; height:140px; position: relative; display: block; z-index: 1; /*to be lowest div*/ background:lime; } #btn{ margin-bottom: 50px; float: right; width: 200px; height: 60px; } .rotate { -webkit-transform: rotateZ(15deg); transform: rotateZ(15deg); } .item, .stillObj { -webkit-transform-style: preserve-3d; transform-style: preserve-3d; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <button type="button" id="btn">Click</button> <div class="stillObj"></div> <div id="outer"> <div class="inner"> <div> <div class="item"></div> </div> <div class="itemBelow"> <div class="innerBelow"></div> </div> </div> </div> 

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

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