I just want to keep z-index order as it is before presing alone-box toggle animation
button.
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
acts like position:relative
(if no position is set) , so z-index
default value comes to be 0 and the child box follows this new stacking context.
The child boxe is indeed z-index:1
but within the parent at z-index:0
. Since it is drawn first, next box drawn on same level will be drawn over it.
an example with static, relative , transform and z-index : http://codepen.io/anon/pen/AXkzOB
you need to set the z-index on the absolute boxe within the assigned class #alone-boxes-wrapper.animated
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>
or once for all in the 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>
more info here : http://webdesign.tutsplus.com/articles/what-you-may-not-know-about-the-z-index-property--webdesign-16892 (shared from @seahorsepip )