css怎么设置淡入文字,如何使用纯CSS实现文本的淡入动画效果(附源码)

本篇文章给大家带来的内容是关于如何使用纯CSS实现文本的淡入动画效果(附源码),有一定的参考价值,有需要的朋友可以参考一下,希望对你有所帮助。

效果预览

2b1427b9920b9bce6ab812cfdaf3b5de.gif

源代码下载

https://github.com/comehope/front-end-daily-challenges

代码解读

定义 dom,容器中包含若干子元素,每个子元素是 1 个字母:

h

a

p

p

y

h

o

l

i

d

a

y

s

居中显示:body {

margin: 0;

height: 100vh;

display: flex;

align-items: center;

justify-content: center;

background: linear-gradient(pink, white, pink);

}

设置字体样式:.container span {

display: inline-block;

color: purple;

font-weight: bold;

text-transform: uppercase;

font-size: 40px;

}

定义文字从左到右的移动效果:.container span {

animation: sideSlide 4s forwards infinite;

transform: translateX(-100vw);

}

@keyframes sideSlide {

15%, 20% {

transform: translateX(0.5em);

}

24% {

transform: translateX(0);

}

25%, 75% {

transform: translateX(0);

}

90%, 100% {

transform: translateX(100vw);

}

}

增加文字缩放的动画效果:.container span {

transform: translateX(-100vw) scale(0);

}

@keyframes sideSlide {

15%, 20% {

transform: translateX(0.5em) scale(1);

}

24% {

transform: translateX(0) scale(1.2);

}

25%, 75% {

transform: translateX(0) scale(1);

}

90%, 100% {

transform: translateX(100vw) scale(0);

}

}

增加文字入场和出场时的淡入淡出效果:.container span {

filter: opacity(0);

}

@keyframes sideSlide {

15%, 20% {

transform: translateX(0.5em) scale(1);

}

24% {

transform: translateX(0) scale(1.2);

}

25%, 75% {

transform: translateX(0) scale(1);

filter: opacity(1);

}

90%, 100% {

transform: translateX(100vw) scale(0);

filter: opacity(0);

}

}

增加文字变色的效果:@keyframes sideSlide {

15%, 20% {

transform: translateX(0.5em) scale(1);

color: purple;

}

24% {

transform: translateX(0) scale(1.2);

color: cyan;

}

25%, 75% {

transform: translateX(0) scale(1);

filter: opacity(1);

color: purple;

}

90%, 100% {

transform: translateX(100vw) scale(0);

filter: opacity(0);

}

}

设置子元素的下标变量:.container span:nth-child(1) { --n: 1; }

.container span:nth-child(2) { --n: 2; }

.container span:nth-child(3) { --n: 3; }

.container span:nth-child(4) { --n: 4; }

.container span:nth-child(5) { --n: 5; }

.container span:nth-child(6) { --n: 6; }

.container span:nth-child(7) { --n: 7; }

.container span:nth-child(8) { --n: 8; }

.container span:nth-child(9) { --n: 9; }

.container span:nth-child(10) { --n: 10; }

.container span:nth-child(11) { --n: 11; }

.container span:nth-child(12) { --n: 12; }

.container span:nth-child(13) { --n: 13; }

.container span:nth-child(14) { --n: 14; }

设置子元素的动画延时:.container span {

animation-delay: calc((var(--n) - 1) * 0.05s);

}

大功告成!

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值