动画时 z-index on::before 的 CSS 问题

标签 css animation z-index pseudo-element

你知道为什么当我为父级设置动画时 z-index 在::before 上不起作用吗?

在这里您可以找到我的意思:http://jsfiddle.net/8x3Wa/6/

.object {
    position: relative;
    z-index: 1;
    -webkit-animation: scale 1s infinite ease-in-out;
    animation: scale 1s infinite ease-in-out;
}

.object::before {
    content: "";
    position: absolute;
    z-index: -1;
}

最佳答案

使用:before创建的元素是.object的子元素;因此你不能将它移到其父级后面。相反,使用 :before:after.object 中创建两个元素,然后根据需要重叠这两个元素。

这是 fiddle :http://jsfiddle.net/8x3Wa/7/ .

HTML:

<div class="object"></div>

CSS:

.object {
   position: relative;

   -webkit-animation: scale 1s infinite ease-in-out;
   animation: scale 1s infinite ease-in-out;
}

.object::after {
    content: "";
    position: absolute;
    top: 0;
    left: 0;
    width: 50px;
    height: 50px;
    border: 20px solid red;
    z-index: 1;
}

.object::before {
    content: "";
    position: absolute;
    width: 70px;
    height: 70px;
    border: 10px solid blue;  
    z-index: -1;
    top: 20px;
    left: 20px;
}


@keyframes scale {
  0%   { transform: scale(1, 1); }
  50%  { transform: scale(.5, .5); }
  100% { transform: scale(1, 1); }}

@-webkit-keyframes scale {
  0%   { -webkit-transform: scale(1, 1); }
  50%  { -webkit-transform: scale(.5, .5); }
  100% { -webkit-transform: scale(1, 1); }
}

关于动画时 z-index on::before 的 CSS 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24519140/

相关文章:

vaadin - 带表格的 CSS,无边框的空行

html - 如何在 Bootstap4 基本 slider 下方放置标题

javascript - 如何让同一类的两个 div 在屏幕上移动?

javascript - 如何在动态图像表上添加叠加标签?

html - CSS 动画不工作(以前工作)

wpf - 为什么 WPF 中的 TextBox.Text 不可设置动画?

css - 覆盖 CSS Z-Index 堆叠上下文

css - 为什么 overflow 与 z-index 交互?

html - iOS 不尊重 z-index 与 -webkit-overflow-scrolling

html - 为什么我的悬停命令不起作用?