我在我正在处理的元素中使用 CSS 动画(来自 animate.css)。 我发现,当在一个容器中淡入一个绝对定位且 z-indexed 的子元素时,子元素的 z-index 没有正常工作。
我在这个 fiddle 中重现了这个问题: http://jsfiddle.net/Lxsf9ako/
问题似乎是由
引起的animation-fill-mode: both;
这个样式是由 animate.css 放置在容器上的,因此我无法控制它。我可以使用 animation-fill-mode: none
覆盖它,但我不想对每个动画都这样做。
对此有什么想法吗?
更新: 我刚刚发现这是与 webkit 相关的,IE11 可以正确呈现它。
更新 2:
我无法在悬停时编辑 .container
类。
更新 3:
Fiddle 中的“悬停”只是一个演示。事实上,.over
对象是来自 angular-ui-bootstrap datepicker 指令的弹出窗口,而 .container
元素是整个应用程序中使用的通用元素。给它们一个额外的 id/class 以将它们标识为 datepicker 容器对我来说不是一个干净的解决方案。
最佳答案
只需添加
#hoverme{
z-index: 1;
}
为什么你必须这样做
Without any z-index value, elements stack in the order that they appear in the DOM (the lowest one down at the same hierarchy level appears on top). Elements with non-static positioning will always appear on top of elements with default static positioning.
Also note that nesting plays a big role. If an element B sits on top of element A, a child element of element A can never be higher than element B.
#hoverme {
z-index: 1;
}
.container {
background: rgb(255, 255, 255);
/* Old browsers */
background: -webkit-gradient(linear, 0 0, 0 100%, from(#fff), to(#efefef));
background: -webkit-linear-gradient(#fff 0%, #efefef 100%);
background: -moz-linear-gradient(#fff 0%, #efefef 100%);
background: -o-linear-gradient(#fff 0%, #efefef 100%);
background: linear-gradient(#fff 0%, #efefef 100%);
filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='#ffffff', endColorstr='#efefef', GradientType=0);
/* IE6-9 */
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
display: block;
width: 100%;
border: 1px solid #c7c7c7;
margin-bottom: 10px;
position: relative;
padding: 20px;
box-sizing: border-box;
-webkit-animation-name: fadeIn;
/* Safari, Chrome and Opera > 12.1 */
-moz-animation-name: fadeIn;
/* Firefox < 16 */
-ms-animation-name: fadeIn;
/* Internet Explorer */
-o-animation-name: fadeIn;
/* Opera < 12.1 */
animation-name: fadeIn;
-webkit-animation-duration: 1s;
-moz-animation-duration: 1s;
-ms-animation-duration: 1s;
-o-animation-duration: 1s;
animation-duration: 1s;
-webkit-animation-fill-mode: both;
-moz-animation-fill-mode: both;
-ms-animation-fill-mode: both;
-o-animation-fill-mode: both;
animation-fill-mode: both;
}
#hoverme .over {
display: none;
padding: 20px;
position: absolute;
top: 20px;
left: 20px;
width: 200px;
background: #efefef;
z-index: 10;
box-sizing: border-box;
-webkit-box-shadow: 2px 2px 4px 0px rgba(0, 0, 0, 0.75);
-moz-box-shadow: 2px 2px 4px 0px rgba(0, 0, 0, 0.75);
box-shadow: 2px 2px 4px 0px rgba(0, 0, 0, 0.75);
}
#hoverme:hover .over {
display: block;
}
@keyframes fadeIn {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
/* Firefox < 16 */
@-moz-keyframes fadeIn {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
/* Safari, Chrome and Opera > 12.1 */
@-webkit-keyframes fadeIn {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
/* Internet Explorer */
@-ms-keyframes fadeIn {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
/* Opera < 12.1 */
@-o-keyframes fadeIn {
0% {
opacity: 0;
}
100% {
opacity: 1;
}
}
<div id="hoverme" class="container">
<div class="over">I should be over the next container</div>
</div>
<div class="container">I should be under the .over</div>
更新
您还可以在 .over
上使用具有 opacity
和 visibility
的过渡。
#hoverme .over {
opacity: 0;
visibility:hidden;
transition:visibility 0.4s, opacity 0.4s;
...
}
#hoverme:hover .over {
visibility:visible;
opacity:1;
}
.container {
background: rgb(255, 255, 255);
/* Old browsers */
background: -webkit-gradient(linear, 0 0, 0 100%, from(#fff), to(#efefef));
background: -webkit-linear-gradient(#fff 0%, #efefef 100%);
background: -moz-linear-gradient(#fff 0%, #efefef 100%);
background: -o-linear-gradient(#fff 0%, #efefef 100%);
background: linear-gradient(#fff 0%, #efefef 100%);
filter: progid: DXImageTransform.Microsoft.gradient(startColorstr='#ffffff', endColorstr='#efefef', GradientType=0);
/* IE6-9 */
font-family: 'Helvetica Neue', Helvetica, Arial, sans-serif;
display: block;
width: 100%;
border: 1px solid #c7c7c7;
margin-bottom: 10px;
position: relative;
padding: 20px;
box-sizing: border-box;
}
#hoverme .over {
opacity: 0;
visibility: hidden;
transition: visibility 0.4s, opacity 0.4s;
padding: 20px;
position: absolute;
top: 20px;
left: 20px;
width: 200px;
background: #efefef;
z-index: 10;
box-sizing: border-box;
-webkit-box-shadow: 2px 2px 4px 0px rgba(0, 0, 0, 0.75);
-moz-box-shadow: 2px 2px 4px 0px rgba(0, 0, 0, 0.75);
box-shadow: 2px 2px 4px 0px rgba(0, 0, 0, 0.75);
}
#hoverme:hover .over {
visibility: visible;
opacity: 1;
}
<div id="hoverme" class="container">
<div class="over">I should be over the next container</div>
</div>
<div class="container">I should be under the .over</div>
关于CSS animation-fill-mode 和 z-index 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26099421/