动态打字效果

<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>循环展示效果(无光标)</title>
  <style>
    /* 页脚容器 */
    .footer-container {
      text-align: center;
      padding: 20px 0;
      font-family: "Helvetica Neue", Arial, sans-serif;
    }

    /* 文字容器 */
    .text-container {
      font-size: 18px;
      color: #666; 
      font-weight: 500;
      display: inline-block;
    }

    /* 单个字符样式(用于打字/删除动画) */
    .char {
      display: inline-block;
      opacity: 0; /* 初始隐藏 */
      transform: translateY(5px); /* 轻微下移 */
      transition: opacity 0.3s ease, transform 0.3s ease;
    }
  </style>
</head>
<body>
  <div class="footer-container">
    <!-- 文字容器(动态生成字符) -->
    <div class="text-container" id="textContainer"></div>
  </div>

  <script>
    const text = "laosu.net"; // 目标文字
    const container = document.getElementById("textContainer");
    let isDeleting = false; // 是否处于删除状态
    let currentIndex = 0; // 当前显示的字符索引

    // 初始化:创建所有字符元素
    [...text].forEach((char, index) => {
      const span = document.createElement("span");
      span.className = "char";
      span.textContent = char;
      span.dataset.index = index; // 记录索引
      container.appendChild(span);
    });
    const chars = document.querySelectorAll(".char");

    // 打字/删除动画函数
    function animateText() {
      if (!isDeleting) {
        // 打字阶段:逐个显示字符
        if (currentIndex < text.length) {
          chars[currentIndex].style.opacity = "1";
          chars[currentIndex].style.transform = "translateY(0)";
          currentIndex++;
          setTimeout(animateText, 150); // 每个字符间隔150ms
        } else {
          // 打完后停顿1秒,再开始删除
          isDeleting = true;
          setTimeout(animateText, 1000);
        }
      } else {
        // 删除阶段:逐个隐藏字符
        if (currentIndex > 0) {
          currentIndex--;
          chars[currentIndex].style.opacity = "0";
          chars[currentIndex].style.transform = "translateY(5px)";
          setTimeout(animateText, 100); // 删除速度稍快(100ms/字符)
        } else {
          // 删完后停顿0.5秒,再开始打字(循环)
          isDeleting = false;
          setTimeout(animateText, 500);
        }
      }
    }

    // 启动循环动画
    animateText();
  </script>
</body>
</html>

 

THE END
喜欢就支持一下吧
点赞5 分享