当前位置: 首页 > news >正文

前端实现打字效果

前端实现打字效果

不带光标

只一次播放

HTML

<!-- 需要在初始化的时候不显示文字 -->
<div id="typing"></div>

CSS

#typing {position: relative;font-size: 24px;font-family: Arial, sans-serif;padding: 10px;
}

JS

const text = "要显示的文本 Hello, World! 要显示的文本"; // 要显示的文本
const typingSpeed = 36; // 打字速度(每个字符的延迟时间)const typingEffect = document.getElementById("typing"); // 获取元素
let index = 0; // 当前显示到字符索引function typeNextCharacter() {if (index < text.length) {// 显示的内容typingEffect.textContent += text.charAt(index);index++;setTimeout(typeNextCharacter, typingSpeed);}
}

无线播放

HTML

<!-- 需要在初始化的时候不显示文字 -->
<div id="typing"></div>

CSS

#typing {position: relative;font-size: 24px;font-family: Arial, sans-serif;padding: 10px;
}

JS

const text = "要显示的文本 Hello, World! 要显示的文本"; // 要显示的文本
const typingSpeed = 36; // 打字速度(每个字符的延迟时间)const typingEffect = document.getElementById("typing"); // 获取元素
let index = 0; // 当前显示到字符索引(function typeNextCharacter() {if (index < text.length) {// 显示的内容typingEffect.textContent += text.charAt(index);index++;setTimeout(typeNextCharacter, typingSpeed);} else {typingEffect.textContent = "";index = 0;setTimeout(typeNextCharacter, typingSpeed);}
})();

在这里插入图片描述

带光标

HTML

<!-- 需要在初始化的时候不显示文字 -->
<div id="typing"></div>

CSS

#typing {position: relative;font-size: 24px;font-family: Arial, sans-serif;padding: 10px;
}
#typing::after {position: absolute;content: "|";animation: typing 1s linear infinite;
}@keyframes typing {0%,49% {opacity: 1;}50%,100% {opacity: 0;}
}

JS

const text = "要显示的文本 Hello, World! 要显示的文本"; // 要显示的文本
const typingSpeed = 36; // 打字速度(每个字符的延迟时间)const typingEffect = document.getElementById("typing"); // 获取元素
let index = 0; // 当前显示到字符索引function typeNextCharacter() {if (index < text.length) {// 显示的内容typingEffect.textContent += text.charAt(index);index++;setTimeout(typeNextCharacter, typingSpeed);} 
}typeNextCharacter();

在这里插入图片描述

http://www.lryc.cn/news/168086.html

相关文章:

  • Unix和Linux、GNU和GPL、RHEL和Centos、Debian和Ubuntu
  • InfiniBand vs 光纤通道,存储协议的选择
  • 第2章_freeRTOS入门与工程实践之单片机程序设计模式
  • python LeetCode 刷题记录 58
  • HarmonyOS开发:那些开发中常见的问题汇总(一)
  • 新能源汽车驱动电机的基本知识
  • 流媒体协议——RTSP
  • Arcgis提取点数据经纬度
  • 【小记录】jupyter notebook新版本
  • Ubuntu安装深度学习环境相关(yolov8-python部署)
  • jmeter采集ELK平台海量业务日志( 采用Scroll)
  • React 全栈体系(五)
  • 动态规划——状态机模型
  • 合宙Air724UG LuatOS-Air LVGL API控件-图片(Gif)
  • 【C语言】指针和数组笔试题解析(2)
  • 3.3 DLL注入:突破会话0强力注入
  • C语言 —— 初步入门知识(内存、指针、结构体)
  • PHP8中字符串与数组的转换-PHP8知识详解
  • Wordtune:文本编辑工具
  • notifyIcon动态图标
  • 2023年墨西哥 SP/BMV IPC 研究报告
  • JWT生成与解析/JWT令牌前端存储
  • [交互]前端展示服务端获取的图片
  • LeetCode2.两数相加
  • Linux编译过程与交叉编译
  • MediaPipe+OpenCV 实现实时手势识别(附Python源码)
  • 为什么选择C/C++内存检测工具AddressSanitizer?如何使用AddressSanitizer?
  • 获取vue当前页面url问号后面的参数
  • Linux编程之线程池的设计与实现
  • stm32---定时器输入捕获