【问题标题】:pseudo-element stacking for animations动画的伪元素堆叠
【发布时间】:2017-02-20 11:53:19
【问题描述】:

我使用伪选择器为 CSS 类设置了涟漪效果。 我希望该动画从元素本身后面运行,但我无法找到如何做到这一点。

.button {
	width: 50px;
	height: 50px;
	background: lightblue;
	position: absolute;
	top: 50px;
	left: 50px;
}
i.ripple:before {
	content: "";
	position: absolute;
	border-radius: 50%;
	width: 30px;
	height: 30px;
	background: darkorange;
	animation: ripple 2s ease-in infinite;
}
@keyframes ripple {
	0% {transform: scale(1);opacity: .5;}
	100% {transform: scale(8);opacity: 0;}
}
<i class="ripple button">test</i>

如果您运行该示例,您会看到橙色圆圈位于 .button 类的蓝色框的顶部,我希望它位于后面。

我认为这个问题与另一个问题有关: ::before pseudo-element stacking order issue 但想不通。

【问题讨论】:

    标签: css animation pseudo-element


    【解决方案1】:

    将其z-index 设置为-1,您应该会很好。

    .button {
    	width: 50px;
    	height: 50px;
    	background: lightblue;
    	position: absolute;
    	top: 50px;
    	left: 50px;
    }
    i.ripple:before {
    	content: "";
    	position: absolute;
    	border-radius: 50%;
    	width: 30px;
    	height: 30px;
    	background: darkorange;
    	animation: ripple 2s ease-in infinite;
    
    	z-index:-1;
    }
    @keyframes ripple {
    	0% {transform: scale(1);opacity: .5;}
    	100% {transform: scale(8);opacity: 0;}
    }
    <i class="ripple button">test</i>

    【讨论】:

    • 嗯,它确实有效,但我的按钮类有一个 z-index: 1; (不在示例中,这是我的错)并且似乎在搞乱这个解决方案
    • @Dunnow 使用这种 html 结构是不可能的。您必须在 .button 内添加一个元素以包含文本并设置背景等,以便伪元素可以放在它后面。由于按钮创建了堆叠顺序(通过定位并具有 z-index,它的子元素不能在其后面,并且伪元素是子元素
    • 好的,我将尝试从我的代码中消除 z-index: 1,然后将 -1 应用于波纹本身。谢谢。
    【解决方案2】:

    更新 Z 索引。

    .button {
    	width: 50px;
    	height: 50px;
    	background: lightblue;
    	position: absolute;
    	top: 50px;
    	left: 50px;
    }
    i.ripple:before {
    	content: "";
    	position: absolute;
    	border-radius: 50%;
    	width: 30px;
    	height: 30px;
    	background: darkorange;
    	animation: ripple 2s ease-in infinite;
      z-index: -1;
    }
    @keyframes ripple {
    	0% {transform: scale(1);opacity: .5;}
    	100% {transform: scale(8);opacity: 0;}
    }
    <i class="ripple button">test</i>

    .button {
    	width: 50px;
    	height: 50px;
    	background: lightblue;
    	position: absolute;
    	top: 50px;
    	left: 50px;
    }
    i.ripple:before {
    	content: "";
    	position: absolute;
    	border-radius: 50%;
    	width: 30px;
    	height: 30px;
    	background: darkorange;
    	animation: ripple 2s ease-in infinite;
    }
    @keyframes ripple {
    	0% {transform: scale(1);opacity: .5;}
    	100% {transform: scale(8);opacity: 0;}
    }
    <i class="ripple button">test</i>

    【讨论】:

      猜你喜欢
      • 2011-12-11
      • 2017-04-27
      • 2012-06-20
      • 1970-01-01
      • 1970-01-01
      • 2019-02-15
      • 1970-01-01
      • 2013-10-09
      • 1970-01-01
      相关资源
      最近更新 更多