2013-12-19 148 views
0

一個字是如何逐字寫入的。它應該會出現一些又一次的信件。這裏是代碼。如何寫一個字母的字母?

var txt = 'animating text'; 
var current = 0; 

function write(text){ 
    var elem = document.getElementById('target'); 
    if (current < text.length){ 
     elem.textContent = elem.textContent + text.charAt(current); 
     current++; 
     wait(100); 
    } 
} 

Write(txt); 
+0

如何確定用戶是否已經讀了這封信沒有?什麼是最終目標?你到現在爲止嘗試過什麼? – Yogesh

+1

這與用戶無關。這裏唯一要做的就是逐個打印字母。如果你看到我的鏈接,它實際上非常吸引人。舉一個例子:「這是一個測試」。 然後首先顯示'T'。在100ms'h'出現後等等...... –

回答

2

基本上你一次寫一個字符,並且在beteween中有一個超時。 Here's the fiddle.和下面的代碼

var text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Pellentesque consequat ante sed fringilla bibendum. In quam elit, rutrum quis adipiscing id, iaculis eu ipsum. Morbi faucibus lectus at ante feugiat dignissim. In turpis turpis, placerat in fringilla eget, sodales blandit lacus. Vivamus tempor blandit mauris nec semper. Sed cursus metus sed justo cursus bibendum. Maecenas ornare sem sed lacinia auctor. Nulla sapien dolor, faucibus vitae dapibus in, commodo id est. Sed a ipsum laoreet, convallis lacus eu, hendrerit magna.'; 

// Variable for current position 
var curr = 0; 

var Write = function write(){ 

    // Find the target element to write to 
    var elem = document.getElementById('target'); 

    // Append next character into the text content 
    elem.textContent = elem.textContent + text.charAt(curr); 

    // Update the current position 
    curr++; 

    // if we're not yet in the end of the string 
    // we have a little (20ms) pause before we write the next character 
    if (curr < text.length) 
     window.setTimeout(write, 20); 
}; 

Write(); // And of course we have to call the function 
+1

這就是我問的東西。完美的答案。 –

相關問題