如何在多个元素变换动画上保持z-index顺序



我只是想保持z指数顺序,因为它是按下alone-box toggle animation按钮之前。jsfiddle

function toggle_class() {if(document.getElementById('alone-boxes-wrapper').className == 'animated') document.getElementById('alone-boxes-wrapper').classList.remove('animated'); else document.getElementById('alone-boxes-wrapper').classList.add('animated');}
div.alone-box {
  position:absolute;
  z-index: 1;
  height: 100px;
  width:100px;
  background-color: #F4F1DE;
  opacity:0.9;
  border:1px dashed #000;
}
div.absolute-wrapper {
  position:absolute;
  top:20px;
}
div.wrapper {
  position:relative;
  height: 100px;
  width:100px;
  left: 50px;
}
div.wrapper .box-1 {
  position:absolute;
  z-index:0;
}
div.wrapper .box-2 {
  position:absolute;
  top: 20px;
  z-index:2;
}
@keyframes animation {
    from { transform: translateX(1rem); }
    to { transform: translateX(-1rem); }
}
div.wrapper.animated .box-1 {
  animation-name: animation;
  animation-duration: 5s;
  animation-iteration-count: infinite;
  animation-direction: alternate;
  animation-timing-function: linear;
}
#alone-boxes-wrapper.animated {
  animation-name: animation;
  animation-duration: 5s;
  animation-iteration-count: infinite;
  animation-direction: alternate;
  animation-timing-function: linear;
}
<div id="alone-boxes-wrapper">
  <div class="alone-box"></div> <!-- z-index: 1 -->
</div>
<div class="absolute-wrapper">
  <div class="wrapper animated">
    <div class="box-1">1111111111</div> <!-- z-index: 0 -->
    <div class="box-2">222222222</div> <!-- z-index: 2 -->
  </div>
</div>
<button type="button" style="position:absolute;top:120px" onClick="toggle_class()">alone-box toggle animation</button>

transform的行为类似于position:relative(如果没有设置位置),因此z-index的默认值为0,子框遵循这个新的堆叠上下文。

子框确实是z-index:1,但在父框z-index:0内。因为它是先绘制的,所以在同一水平面上绘制的下一个框将绘制在它上面。

一个带有静态、相对、转换和z-index的示例:http://codepen.io/anon/pen/AXkzOB

你需要在指定的类#alone-boxes-wrapper.animated

内的绝对框上设置z-index

function toggle_class() {if(document.getElementById('alone-boxes-wrapper').className == 'animated') document.getElementById('alone-boxes-wrapper').classList.remove('animated'); else document.getElementById('alone-boxes-wrapper').classList.add('animated');}
div.alone-box {
  position:absolute;
  z-index: 1;
  height: 100px;
  width:100px;
  background-color: #F4F1DE;
  opacity:0.9;
  border:1px dashed #000;
}
div.absolute-wrapper {
  position:absolute;
  top:20px;
}
div.wrapper {
  position:relative;
  height: 100px;
  width:100px;
  left: 50px;
}
div.wrapper .box-1 {
  position:absolute;
  z-index:0;
}
div.wrapper .box-2 {
  position:absolute;
  top: 20px;
  z-index:2;
}
@keyframes animation {
    from { transform: translateX(1rem); }
    to { transform: translateX(-1rem); }
}
div.wrapper.animated .box-1 {
  animation-name: animation;
  animation-duration: 5s;
  animation-iteration-count: infinite;
  animation-direction: alternate;
  animation-timing-function: linear;
}
#alone-boxes-wrapper.animated {
  z-index: 1;/* here */
  animation-name: animation;
  animation-duration: 5s;  
  animation-iteration-count: infinite;
  animation-direction: alternate;
  animation-timing-function: linear;
}
<div id="alone-boxes-wrapper">
  <div class="alone-box"></div> <!-- z-index: 1 -->
</div>
<div class="absolute-wrapper">
  <div class="wrapper animated">
    <div class="box-1">1111111111</div> <!-- z-index: 0 -->
    <div class="box-2">222222222</div> <!-- z-index: 2 -->
  </div>
</div>
<button type="button" style="position:absolute;top:120px" onClick="toggle_class()">alone-box toggle animation</button>

或一次性在id #alone-boxes-wrapper

function toggle_class() {if(document.getElementById('alone-boxes-wrapper').className == 'animated') document.getElementById('alone-boxes-wrapper').classList.remove('animated'); else document.getElementById('alone-boxes-wrapper').classList.add('animated');}
div.alone-box {
  position:absolute;
  z-index: 1;
  height: 100px;
  width:100px;
  background-color: #F4F1DE;
  opacity:0.9;
  border:1px dashed #000;
}
div.absolute-wrapper {
  position:absolute;
  top:20px;
}
div.wrapper {
  position:relative;
  height: 100px;
  width:100px;
  left: 50px;
}
div.wrapper .box-1 {
  position:absolute;
  z-index:0;
}
div.wrapper .box-2 {
  position:absolute;
  top: 20px;
  z-index:2;
}
@keyframes animation {
    from { transform: translateX(1rem); }
    to { transform: translateX(-1rem); }
}
div.wrapper.animated .box-1 {
  animation-name: animation;
  animation-duration: 5s;
  animation-iteration-count: infinite;
  animation-direction: alternate;
  animation-timing-function: linear;
}
#alone-boxes-wrapper.animated {
  animation-name: animation;
  animation-duration: 5s;
  animation-iteration-count: infinite;
  animation-direction: alternate;
  animation-timing-function: linear;
}
#alone-boxes-wrapper {
      z-index: 1;/* here */
  }
<div id="alone-boxes-wrapper">
  <div class="alone-box"></div> <!-- z-index: 1 -->
</div>
<div class="absolute-wrapper">
  <div class="wrapper animated">
    <div class="box-1">1111111111</div> <!-- z-index: 0 -->
    <div class="box-2">222222222</div> <!-- z-index: 2 -->
  </div>
</div>
<button type="button" style="position:absolute;top:120px" onClick="toggle_class()">alone-box toggle animation</button>

更多信息在这里:http://webdesign.tutsplus.com/articles/what-you-may-not-know-about-the-z-index-property--webdesign-16892(分享自@seahorsepip)

最新更新