2014-06-19 85 views
2

我想要创建一个jquery脚本,可以按字符在div字符内写下一个字符串,就好像有人在用户正在查看页面时输入它一样。jquery按字符显示字符串

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

请帮我解决这个问题。谢谢。

+0

这个主题有一些例子,一些链接到一些插件:http://stackoverflow.com/questions/2578211/how-do-you-simulate使用-typing- - jquery的 – dumdum

回答

1

你可以自己写一个。

  • 。利用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:

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

jQuery的打字机可以在这里找到:https://github.com/chadselph/jquery-typewriter

你可以看到回购本身的演示。

这里是它如何工作的例子:

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