css制作动画文字
CSS 制作动画文字的方法
使用 @keyframes 定义动画
通过 @keyframes 规则定义动画的关键帧,控制文字在不同时间点的样式变化。例如,实现文字颜色渐变和缩放效果:
@keyframes textAnimation {
0% {
color: red;
transform: scale(1);
}
50% {
color: blue;
transform: scale(1.2);
}
100% {
color: green;
transform: scale(1);
}
}
应用动画到文字元素
将定义好的动画绑定到目标文字元素,设置动画的持续时间、延迟、循环方式等属性:

.animated-text {
animation-name: textAnimation;
animation-duration: 3s;
animation-delay: 0.5s;
animation-iteration-count: infinite;
animation-timing-function: ease-in-out;
}
使用 transition 实现悬停效果
通过 transition 属性为文字添加简单的悬停动画,例如颜色或位置变化:

.hover-text {
color: black;
transition: color 0.3s ease, transform 0.3s ease;
}
.hover-text:hover {
color: purple;
transform: translateY(-5px);
}
文字闪烁效果
结合 opacity 属性实现文字闪烁:
@keyframes blink {
0%, 100% { opacity: 1; }
50% { opacity: 0; }
}
.blink-text {
animation: blink 2s infinite;
}
文字打字机效果
通过控制 width 或 border-right 模拟打字机效果:
@keyframes typing {
from { width: 0; }
to { width: 100%; }
}
.typewriter {
overflow: hidden;
white-space: nowrap;
animation: typing 3s steps(40) forwards;
}
注意事项
- 确保动画性能优化,避免过多使用
box-shadow或filter等耗性能的属性。 - 使用
will-change属性提前告知浏览器可能变化的属性(如transform)。 - 为旧版本浏览器添加
-webkit-等前缀以保证兼容性。
通过组合以上方法,可以实现从简单到复杂的文字动画效果。






