基于CSS animation-timing-function实现逐字循环渐入渐出动画的问题
原代码的核心问题
- 关键帧仅定义了
opacity:0 → opacity:1的单向过渡,依靠alternate属性触发反向播放时,没有预留文字停留的时间区间,容易出现从全亮直接跳回透明的生硬闪烁感。 - 整行文字绑定同一个动画配置,同步启动过渡效果,无法实现按顺序逐个渐入的预期节奏。
- 没有在关键帧中闭合“渐入-停留-渐出”的完整流程,循环播放时的过渡顺滑度不足。
可直接复用的修正实现
核心调整逻辑
- 重写关键帧时间轴:在单个动画周期内完成从透明到不透明、短暂停留、再回到透明的全流程,不再依赖
alternate属性做反向播放,消除跳变感。 - 拆分文字单元:将整行文本拆分为独立的块级元素,给每个元素设置递增的动画延迟,形成逐字依次淡入的节奏。
- 保留
infinite循环配置,让整个流程无间断往复运行。
完整代码
CSS 样式
/* 文本组基础样式 */ .text-container { font-size: 2rem; margin: 2rem; } /* 单字通用动画配置 */ .text-single { display: inline-block; opacity: 0; /* 动画参数:绑定关键帧、无限循环、单周期时长2s、ease缓动 */ animation: fadeCycle 2s infinite ease; } /* 逐字设置递增延迟,拉开出现的时间差 */ .text-single:nth-child(1) { animation-delay: 0s; } .text-single:nth-child(2) { animation-delay: 0.12s; } .text-single:nth-child(3) { animation-delay: 0.24s; } .text-single:nth-child(4) { animation-delay: 0.36s; } .text-single:nth-child(5) { animation-delay: 0.48s; } .text-single:nth-child(6) { animation-delay: 0.6s; } .text-single:nth-child(7) { animation-delay: 0.72s; } .text-single:nth-child(8) { animation-delay: 0.84s; } .text-single:nth-child(9) { animation-delay: 0.96s; } .text-single:nth-child(10) { animation-delay: 1.08s; } /* 单周期完整动画逻辑:透明→全显→停留→透明 */ @keyframes fadeCycle { 0% { opacity: 0; } 30% { opacity: 1; } 70% { opacity: 1; } 100% { opacity: 0; } }
HTML 结构
<h1 class="text-container"> <span class="text-single">I</span> <span class="text-single">t</span> <span class="text-single">s</span> <span class="text-single"> </span> <span class="text-single">j</span> <span class="text-single">u</span> <span class="text-single">s</span> <span class="text-single">t</span> <span class="text-single"> </span> <span class="text-single">f</span> <span class="text-single">a</span> <span class="text-single">d</span> <span class="text-single">e</span> <span class="text-single"> </span> <span class="text-single">i</span> <span class="text-single">n</span> <span class="text-single"> </span> <span class="text-single">n</span> <span class="text-single">o</span> <span class="text-single">t</span> <span class="text-single"> </span> <span class="text-single">o</span> <span class="text-single">u</span> <span class="text-single">t</span> </h1>
灵活调参参考
- 调整单周期流畅度:修改
animation属性中的时长值,数值越大动画越平缓,数值越小节奏越快。 - 调整逐字出现间隔:修改每个
.text-single的animation-delay差值,差值越小逐字衔接越紧凑,差值越大逐字的错落感越强。 - 切换缓动手感:替换不同的
animation-timing-function参数,可选值包括ease、ease-in、ease-out、ease-in-out或自定义cubic-bezier()值,匹配需要的过渡效果。 - 调整文字停留时长:修改关键帧中30%和70%两个时间点的数值,比如把停留区间改成40%到60%,文字全显的持续时间就会缩短。
内容的提问来源于stack exchange,提问作者Aziz
相关产品推荐
相关产品推荐

