jquery逐字符显示字符串 [英] jquery display string character by character

查看:73
本文介绍了jquery逐字符显示字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想创建一个jquery脚本,它可以逐个字符地记下div中的字符串,好像有人在用户正在查看页面时键入它。

I want to create a jquery script that can write down a string inside a div character by character as if somebody would be typing it as the user is looking at the page.

我认为这可以使用settimeout的递归函数。

I assume this would work with a recursive function using settimeout.

请帮帮我。谢谢。

推荐答案

你可以自己写一个。


  • 使用 setInterval()

  • 在数组中存储消息并逐个弹出单词/字符

  • 完成后清除间隔

jQuery:

// Consturct a message, transform it to an array using .split() and reverse it
// If you want to print out one word at a time instead of a character at a time
// Change .split('') to .split(' ')
var $message = 'This is a message to be rendered'.split('').reverse();

// Set the frequency of your 'pops'
var $timeout = 1000;

var outputSlowly = setInterval(function() {

    // Add text to the target element
    $('#target').append($message.pop());

    // No more characters - exit
    if ($message.length === 0) {            
        clearInterval(outputSlowly);   
    }

}, $timeout);

HTML:

<p id="target"></p>

这篇关于jquery逐字符显示字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆