• 前端实现打字效果


    前端实现打字效果

    不带光标

    只一次播放

    HTML

    
    <div id="typing">div>
    
    • 1
    • 2

    CSS

    #typing {
        position: relative;
        font-size: 24px;
        font-family: Arial, sans-serif;
        padding: 10px;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    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);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    无线播放

    HTML

    
    <div id="typing">div>
    
    • 1
    • 2

    CSS

    #typing {
        position: relative;
        font-size: 24px;
        font-family: Arial, sans-serif;
        padding: 10px;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    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);
        }
    })();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18

    在这里插入图片描述

    带光标

    HTML

    
    <div id="typing">div>
    
    • 1
    • 2

    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;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    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();
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

    在这里插入图片描述

  • 相关阅读:
    Django03_Django基本配置
    解构 赋值
    JavaScript双for循环,判断对象数组的阈值重合。
    基于R语言地理加权回归、主成份分析、判别分析等空间异质性数据分析
    【LeetCode力扣】LCR170 使用归并排序的思想解决逆序对问题(详细图解)
    SPA项目开发之首页导航+左侧菜单
    解决sleuth链路追踪失效的问题,sleuth版本升级为3.1.3后X-B3-TraceId:-打印不出来了,解决方案
    MySQL对日期计算
    fastspar微生物相关性推断
    kafka连接图形化工具(Offset Explorer和CMAK)
  • 原文地址:https://blog.csdn.net/weixin_46533577/article/details/132922488