代码 一个简单的打字机效果的实现

2024-11-19 04:47:27 +0800 CST views 522

一个简单的打字机效果的实现

该文本展示了一个简单的打字机效果的实现,使用HTML、CSS和JavaScript。通过CSS样式定义了打字机效果的外观和动画,JavaScript用于动态设置文本和控制动画的重置与执行。每隔5秒,文本会重新执行打字机效果,增强了用户体验。

<!DOCTYPE html>
<html lang="zh">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>打字机</title>
  <style>
    /* 样式部分 */
    .typewriter-effect {
      display: flex;
      justify-content: center;
      align-items: center;
      font-family: monospace;
      font-size: 24px;
      border-right: 2px solid; /* 光标边框 */
      white-space: nowrap;
      overflow: hidden;
    }

    .typewriter-effect > .text {
      max-width: 0;
      animation: typing 3s linear forwards; /* 使用linear取代steps */
      white-space: nowrap;
      overflow: hidden;
    }

    .typewriter-effect::after {
      content: " |";
      animation: blink 1s infinite;
      animation-timing-function: step-end;
    }

    @keyframes typing {
      0% {
        max-width: 0;
      }
      100% {
        max-width: 100%; /* 平滑过渡到100% */
      }
    }

    @keyframes blink {
      0%, 75%, 100% {
        opacity: 1;
      }
      25% {
        opacity: 0;
      }
    }
  </style>
</head>
<body>
  <!-- HTML部分 -->
  <div class="typewriter-effect">
    <div class="text" id="typewriter-text"></div>
  </div>

  <!-- JavaScript部分 -->
  <script>
    const typeWriter = document.getElementById('typewriter-text');
    const text = '我是程序员茄子'; // 定义要显示的中文文本

    // 设置文本并启动打字动画
    function startTyping() {
      typeWriter.innerHTML = text;
      // 重置动画
      typeWriter.style.animation = 'none';
      setTimeout(() => {
        typeWriter.style.animation = '';
        typeWriter.style.animation = 'typing 3s linear forwards'; // 平滑显示文字
      }, 50);
    }

    // 每隔 5 秒重新执行打字机效果
    startTyping();
    setInterval(startTyping, 5000); // 调整间隔时间使其与动画持续时间匹配
  </script>
</body>
</html>

复制全文 生成海报 前端开发 网页设计 动画效果

推荐文章

liunx服务器监控workerman进程守护
2024-11-18 13:28:44 +0800 CST
Go 中的单例模式
2024-11-17 21:23:29 +0800 CST
Go语言中的mysql数据库操作指南
2024-11-19 03:00:22 +0800 CST
Gin 与 Layui 分页 HTML 生成工具
2024-11-19 09:20:21 +0800 CST
Python设计模式之工厂模式详解
2024-11-19 09:36:23 +0800 CST
如何在Vue 3中使用Ref访问DOM元素
2024-11-17 04:22:38 +0800 CST
HTML + CSS 实现微信钱包界面
2024-11-18 14:59:25 +0800 CST
html一个包含iPhoneX和MacBook模拟器
2024-11-19 08:03:47 +0800 CST
Vue3结合Driver.js实现新手指引功能
2024-11-19 08:46:50 +0800 CST
Nginx rewrite 的用法
2024-11-18 22:59:02 +0800 CST
前端如何给页面添加水印
2024-11-19 07:12:56 +0800 CST
什么是Vue实例(Vue Instance)?
2024-11-19 06:04:20 +0800 CST
利用图片实现网站的加载速度
2024-11-18 12:29:31 +0800 CST
18个实用的 JavaScript 函数
2024-11-17 18:10:35 +0800 CST
五个有趣且实用的Python实例
2024-11-19 07:32:35 +0800 CST
#免密码登录服务器
2024-11-19 04:29:52 +0800 CST
PHP 8.4 中的新数组函数
2024-11-19 08:33:52 +0800 CST
Vue3 vue-office 插件实现 Word 预览
2024-11-19 02:19:34 +0800 CST
Vue3中的Scoped Slots有什么改变?
2024-11-17 13:50:01 +0800 CST
Vue3中的Store模式有哪些改进?
2024-11-18 11:47:53 +0800 CST
java MySQL如何获取唯一订单编号?
2024-11-18 18:51:44 +0800 CST
goctl 技术系列 - Go 模板入门
2024-11-19 04:12:13 +0800 CST
程序员茄子在线接单