jquery逐字符显示字符串

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

我假设这将使用settimeout的递归函数。

请帮帮我。 谢谢。

你可以自己写一个。

  • 利用setInterval()
  • 在数组中存储消息并逐个弹出单词/字符
  • 完成后清除间隔

DEMO

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:

 

jQuery打字机可在此处获取: https : //github.com/chadselph/jquery-typewriter 。

您可以看到repo本身的演示。

以下是它的工作原理示例:

 $('.someselector').typewrite({ 'delay': 100, 'extra_char': '', 'trim': true, 'callback': null });