跳转到内容

文字连续光影特效

刀刀

1/3/2025

0 字

0 分钟

颜色变化

想要实现这个效果,我们可以先拆分成实现一个文字的效果,实现后再考虑多个文字的实现。

查看效果图,它改变的是文字的颜色和阴影效果,且没有通过鼠标就能实现,因此使用动画来实现这个效果。

配置一个动画效果,修改其文字颜色、文字阴影,该动画是无限循环的。但是动画结束后需要反向返回起点,而不是直接变成白色。代码如下:

css
span {
  color: #fff;
  animation: spread 1s ease-in-out infinite alternate;
}

@keyframes spread {
  to {
    color: skyblue;
    text-shadow: 20px 0 70px skyblue;
  }
}

动画延迟

现在文字动画效果已经实现了,不过是整体一起变化,现在需要把他循环设置动画延迟。

这里使用的是 scss ,代码如下:

scss
@for $i from 1 through 8 {
  span:nth-child(#{$i}) {
    animation-delay: ($i - 1) * 0.2s;
  }
}

最终编译成 css 代码如下:

css
span:nth-child(1) {
  animation-delay: 0s;
}

span:nth-child(2) {
  animation-delay: 0.2s;
}

span:nth-child(3) {
  animation-delay: 0.4s;
}

span:nth-child(4) {
  animation-delay: 0.6s;
}

span:nth-child(5) {
  animation-delay: 0.8s;
}

span:nth-child(6) {
  animation-delay: 1s;
}

span:nth-child(7) {
  animation-delay: 1.2s;
}

span:nth-child(8) {
  animation-delay: 1.4s;
}

总体效果