【发布时间】:2020-01-10 22:46:24
【问题描述】:
我正在尝试应用我在视频here 中看到的一个很酷的“故障效果”:
但是,我似乎无法让这个动画工作。这是我试图应用效果的标题元素:
<h1 class="header-block-heading-primary">Web Developer</h1>
这里是 SCSS:
.header-block-heading-primary {
// animation: glitch-effect 3s infinite;
position: relative;
&::before,
&::after {
position: absolute;
left: 0;
top: 0;
content: "";
display: block;
height: 100%;
width: 100%;
z-index: -1;
}
&::before {
color: red;
animation: glitch-effect 3s infinite;
}
&::after {
color: blue;
animation: glitch-effect 2s infinite;
}
}
这是动画:
@keyframes glitch-effect {
0% {
left: -3px;
top: -3px;
}
25% {
left: 3px;
top: 0px;
}
50% {
left: -2px;
top: 3px;
}
75% {
left: 2px;
top: -2px;
}
100% {
left: 0px;
top: -3px;
}
}
这是输出的 CSS:
.header-block-heading-primary {
position: relative;
}
.header-block-heading-primary::before,
.header-block-heading-primary::after {
position: absolute;
left: 0;
top: 0;
content: "";
display: block;
height: 100%;
width: 100%;
z-index: -1;
}
.header-block-heading-primary::before {
color: red;
-webkit-animation: glitch-effect 3s infinite;
animation: glitch-effect 3s infinite;
}
.header-block-heading-primary::after {
color: blue;
-webkit-animation: glitch-effect 2s infinite;
animation: glitch-effect 2s infinite;
}
我遵循了与教程相同的设置,甚至参考了我的一些旧项目来查看::before 和::after 的使用,它们使用几乎相同的代码(对于伪元素)工作得很好.
我只尝试了单个分号,所以:before 和:after,但没有奏效。我将动画直接添加到元素本身(如 .header-block-heading-primary 选择器下方注释掉的 // animation: glitch-effect 3s infinite; 所见),它工作正常,所以我被引导相信 ::before 和 ::after 元素不是在职的。在嵌套的 SCSS 中手动添加 -webkit- 也不起作用。
我查看了网站上的多个其他帖子,但找不到帮助我解决此问题的答案。因此,我们将不胜感激任何帮助!
【问题讨论】:
标签: css animation sass pseudo-element