前端实现打字效果

news/2024/7/19 15:17:59 标签: 前端, js

前端实现打字效果

不带光标

只一次播放

HTML

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

CSS

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

JS

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

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

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.niftyadmin.cn/n/5029500.html

相关文章

vue之封装预约类组件

vue之封装预约类组件 vue之封装预约类组件 vue之封装预约类组件 预约类时间段选择组件的开发思路&#xff1a; 0. 展示所有数据&#xff0c;【禁用】的展示【灰色】&#xff0c;【没选中】的展示【白色】&#xff0c;【选中的】时间范围展示【红色】 1. 点击两次&#xff0c;锁…

央媒发稿不能改?媒体发布新闻稿有哪些注意点

传媒如春雨&#xff0c;润物细无声&#xff0c;大家好&#xff0c;我是51媒体网胡老师。 “央媒发稿不能改”是媒体行业和新闻传播领域的普遍理解。央媒&#xff0c;即中央主要媒体&#xff0c;是权威性的新闻源&#xff0c;当这些媒体发布新闻稿或报道时&#xff0c;其他省、…

Angular变更检测机制

前段时间遇到这样一个 bug&#xff0c;通过一个 click 事件跳转到一个新页面&#xff0c;新页面迟迟不加载&#xff1b; 经过多次测试发现&#xff0c;将鼠标移入某个 tab &#xff0c;页面就加载出来了。 举个例子&#xff0c;页面内容无法加载&#xff0c;但是将鼠标移入下图…

《算法图解》阅读笔记

前言 问题解决技巧&#xff1a;分而治之 / 动态规划&#xff1b;贪婪算法书目&#xff1a;Grokking algorithms: an illustrated guide for programmers and other curious people中文名称&#xff1a;《算法图解——像小说一样有趣的算法入门书》 1 算法简介 二分查找&…

FiddlerScript 脚本使用正则表达式替换响应内容

使用 Fiddler 进行抓包时&#xff0c;对特定 url 接口响应内容进行部分匹配替换处理&#xff0c;可以在 FiddlerScript 的 OnBeforeResponse 事件中编写代码来完成。 本文如下代码示例是对某接口请求中的特定内容进行替换处理&#xff0c;如下&#xff1a; static function On…

学习路之工具----GitHubDesktop安装

学习路之工具----GitHubDesktop安装 一、直接官网下载安装 https://desktop.github.com/二、常见问题解决1、安装报需要装.net 4.5 ,然后安装失败 一、直接官网下载安装 https://desktop.github.com/ 可能慢 可链接: https://pan.baidu.com/s/16A-zcquoQhjCWBmAkXHa_w?pwdjh7…

算法训练 第二周

二、反转链表 本题给我们了一个单链表的头节点head&#xff0c;要求我们把这个单链表的连接顺序进行逆置&#xff0c;并返回逆置后的链表头节点。 1.头插法 我们需要先创建一个新的头节点ph&#xff0c;然后遍历给出的单链表&#xff0c;把遍历到的每一个节点用头插法接到ph的…

【Effective Python】读书笔记-05类与接口

1. 让组合起来的类来实现多层结构&#xff0c;不用使用嵌套的内置类型 2. 让简单的接口接收函数&#xff0c;而不是类的实例 from collections import defaultdictcurrent {a: 1,b: 2,c: 3, }add_to_current {f: 4,e: 5, }def increment():count 0def missing():nonlocal …