You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让现有CSS轮播文本动画实现正常无限循环效果

调整思路

原来的代码无法实现无限循环的核心问题有两点:

  • 单独给最后一个文本设置了rotate-last动画,结束后会固定保持显示状态,无法和第一个文本衔接循环
  • 每个动画的时长和延迟没有对齐整个轮播的总周期,直接加infinite会导致各文本动画节奏错位,出现重叠

具体调整逻辑:

  1. 统一所有轮播文本使用同一个动画,删除专属最后一个文本的rotate-last动画
  2. 计算整个轮播的单轮总时长(3个文本时设置总周期为4.5s,每个文本的可见窗口和错开延迟都对齐这个总周期)
  3. 给所有轮播文本添加animation-iteration-count: infinite属性,保证循环执行

完整可运行示例

直接复制即可运行:

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <title>无限循环轮播文本</title>
  <style>
    .rotatingText {
      font-family: "Georgia", serif;
      font-style: italic;
      font-size: 18px;
      text-align: center;
      position: relative;
      height: 150px; /* 给轮播区域预留高度,避免页面布局偏移 */
      line-height: 150px;
    }

    .rotatingText-adjective {
      font-family: "Open Sans", sans-serif;
      font-size: 50px;
      font-style: normal;
      font-weight: 700;
      left: 0;
      margin: 0;
      opacity: 0;
      position: absolute;
      right: 0;
      text-align: center;
      text-transform: uppercase;
      top: 0;
      /* 统一动画配置 */
      animation-name: rotate;
      animation-duration: 4.5s; /* 单轮总时长 = 文本数量 * 单个文本1.5s的占位周期 */
      animation-iteration-count: infinite;
    }

    /* 每个文本错开启动时间,刚好依次轮播 */
    .rotatingText-adjective:nth-of-type(1) {
      animation-delay: 0s;
    }

    .rotatingText-adjective:nth-of-type(2) {
      animation-delay: 1.5s;
    }

    .rotatingText-adjective:nth-of-type(3) {
      animation-delay: 3s;
    }

    @keyframes rotate {
      0% {
        opacity: 0;
        transform: translate3d(0, 50px, 0);
      }
      11%, 22% { 
        opacity: 1;
        transform: translate3d(0, 0, 0);
      }
      33% {
        opacity: 0;
        transform: translate3d(0, -25px, 0);
      }
      100% {
        opacity: 0;
      }
    }
  </style>
</head>
<body>
  <div class="rotatingText">
    我们的产品
    <div class="rotatingText-adjective">高效</div>
    <div class="rotatingText-adjective">稳定</div>
    <div class="rotatingText-adjective">安全</div>
  </div>
</body>
</html>

自定义调整说明

如果需要增减轮播文本的数量,只要调整两个参数即可:

  • 修改.rotatingText-adjective的animation-duration为 文本数量 * 1.5s
  • 新增的文本依次累加1.5s的animation-delay
  • 同步调整@keyframes里的百分比节点,保证每个文本的显示区间对齐1/总数量的占比即可

内容的提问来源于stack exchange,提问作者Mount Mario

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.26 21:24:05