• 前端实现打字效果


    前端实现打字效果

    不带光标

    只一次播放

    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

    在这里插入图片描述

  • 相关阅读:
    BSAD检验比特币泡沫生成的时间点
    小程序如何搭建在服务器上
    Jenkins pipeline流程控制选项
    MySQL 用 BETWEEN AND 日期查询包含范围边界
    Unity C# 网络学习(十二)——Protobuf生成协议
    企业积分运营体系如何实现积分促活?
    【模电实验】【验证性实验——单管共发射极放大电路实验】
    PowerCLi 通过自建PXE Server实现vCenter批量部署常规New-VM到所有的esxi主机上
    周记之重新开始
    二叉树的概念和性质
  • 原文地址:https://blog.csdn.net/weixin_46533577/article/details/132922488