css制作动画文字
使用 @keyframes 定义动画
通过 @keyframes 规则定义动画的关键帧,设置文字在不同时间点的样式变化。例如:
@keyframes textAnimation {
0% { opacity: 0; transform: translateY(20px); }
100% { opacity: 1; transform: translateY(0); }
}
应用动画到文字元素
将定义好的动画绑定到目标文字元素,设置动画持续时间、延迟、循环方式等属性:
.animated-text {
animation-name: textAnimation;
animation-duration: 1.5s;
animation-timing-function: ease-out;
animation-iteration-count: infinite; /* 可选:无限循环 */
}
使用 transition 实现悬停效果
通过 transition 为文字添加交互动画,例如悬停时颜色或大小变化:
.hover-text {
transition: color 0.3s, transform 0.3s;
}
.hover-text:hover {
color: #ff0000;
transform: scale(1.1);
}
结合 text-shadow 创建发光效果
通过动态改变 text-shadow 实现文字发光或闪烁动画:
@keyframes glow {
0% { text-shadow: 0 0 5px #fff; }
50% { text-shadow: 0 0 20px #ff00ff; }
100% { text-shadow: 0 0 5px #fff; }
}
.glow-text {
animation: glow 2s infinite;
}
实现打字机效果
通过 steps() 函数和 overflow: hidden 模拟逐字显示效果:

@keyframes typing {
from { width: 0; }
to { width: 100%; }
}
.typewriter {
overflow: hidden;
white-space: nowrap;
animation: typing 3s steps(40) forwards;
}
注意事项
- 使用
will-change属性优化动画性能(如will-change: transform)。 - 避免过多复杂动画同时运行,可能导致页面卡顿。
- 通过
@media查询为不同设备调整动画参数。






