2014-06-19 88 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 
});