纯 JavaScript 逐字显示效果

云游道人 2025-04-15 624 阅读 0评论

纯 JavaScript 逐字显示效果(循环打字机效果)

通过 setInterval 实现逐字输出,支持动态速度控制

一次性打字机效果

js示例函数代码:

function typeWriter(text, elementId, speed = 100) {    let i = 0;    const container = document.getElementById(elementId);    container.innerHTML = "";        const timer = setInterval(() => {        if (i >= text.length) {            clearInterval(timer);            return;        }        container.innerHTML += text.charAt(i);        i++;    }, speed); } // 调用示例 typeWriter("欢迎访问本站!", "text-container");

循环打字机效果

js示例函数代码:

function typeWriter(text, elementId, speed = 100) {

    let i = 0;

    let isDeleting = false;

    const container = document.getElementById(elementId);

    let timer;


    function startTyping() {

        timer = setInterval(() => {

            if (!isDeleting) {

                // 打字阶段

                if (i <= text.length) {

                    container.innerHTML = text.substring(0, i);

                    i++;

                } else {

                    clearInterval(timer); // 清除打字定时器

                    isDeleting = true;

                    // 开启删除前等待1秒

                    setTimeout(() => startDeleting(), 3000);

                }

            }

        }, speed);

    }


    function startDeleting() {

        timer = setInterval(() => {

            if (i > 0) {

                container.innerHTML = text.substring(0, --i);

            } else {

                clearInterval(timer);

                isDeleting = false;

                typeWriter(text, elementId, speed); // 重新循环

            }

        }, speed / 2);

    }

    startTyping();

}

增强代码,代码添加了循环开关和删除文字开关

js示例代码:

function typeWriter(text, elementId, speed = 100, loop = true, del = true) {

    let i = 0;

    let isDeleting = false;

    const container = document.getElementById(elementId);

    let timer;


    function startTyping() {

        timer = setInterval(() => {

            if (!isDeleting) {

                if (i <= text.length) {

                    container.innerHTML = text.substring(0, i);

                    i++;

                } else {

                    clearInterval(timer);

                    isDeleting = true;

                    setTimeout(() => startDeleting(), 3000); // 保持暂停逻辑

                }

            }

        }, speed);

    }


    function startDeleting() {

        timer = setInterval(() => {

            if (i > 0) {

                // 新增判断打完字后是否删除

                if (loop || del) container.innerHTML = text.substring(0, --i);

            } else {

                clearInterval(timer);

                isDeleting = false;

                // 新增循环判断

                if (loop) typeWriter(text, elementId, speed, loop); // 

            }

        }, speed / 2);

    }


    startTyping();

}

// 调用示例

typeWriter("欢迎访问本站!", "textcenten");


<p class="typewriter" id="textcenten"></p>


光标动画增强

添加 CSS 光标闪烁效果:

.typewriter::after {    content: "|";    animation: blink 1s infinite; } @keyframes blink { 50% { opacity: 0; } }

喜欢就支持以下吧
点赞 0

发表评论

快捷回复: 表情:
aoman baiyan bishi bizui cahan ciya dabing daku deyi doge fadai fanu fendou ganga guzhang haixiu hanxiao zuohengheng zhuakuang zhouma zhemo zhayanjian zaijian yun youhengheng yiwen yinxian xu xieyanxiao xiaoku xiaojiujie xia wunai wozuimei weixiao weiqu tuosai tu touxiao tiaopi shui se saorao qiudale qinqin qiaoda piezui penxue nanguo liulei liuhan lenghan leiben kun kuaikule ku koubi kelian keai jingya jingxi jingkong jie huaixiao haqian aini OK qiang quantou shengli woshou gouyin baoquan aixin bangbangtang xiaoyanger xigua hexie pijiu lanqiu juhua hecai haobang caidao baojin chi dan kulou shuai shouqiang yangtuo youling
提交
评论列表 (有 0 条评论, 624人围观)

最近发表

热门文章

最新留言

热门推荐

标签列表