2012-11-26 23 views
0

這個jQuery功能通過信字改變從字母打字機的jQuery效果的工作原理是字母大寫的,但我似乎無法得到它的字去字。通過字

任何想法,這裏的功能是:

//Typewriter Effect 
$.fn.Typewriter = function(opts){ 
    var $this = this, 
     defaults = { animDelay: 50 }, 
     settings = $.extend(defaults, opts); 
    $.each(settings.text, function(i, letter){ 
     setTimeout(function(){ 
      $this.html($this.html() + (letter!='\n'?letter:'<br />')); 
     }, settings.animDelay * i); 
    }); 
} 

謝謝!

+1

我很想這樣做,但我必須去睡覺。所以這裏有一個線索:使用settings.text.split(」「),讓您的文字的話 – Popnoodles

回答

0

請勿使用$.each迭代字符串,並使用.text來設置文本而不是.html

若要更改單個字符的效果,整個單詞,只是split輸入文本關鍵詞:

$.fn.Typewriter = function(opts){ 
    var $this = this, 
     defaults = { animDelay: 50 }, 
     settings = $.extend(defaults, opts), 
     words = settings.text.split(/(?=\s)/), // split before every whitespace 
     i = 0; 
    function animate() { 
     if (i==words.length) return; 
     $this.text(words.slice(0, i++).join('')); 
     setTimeout(animate, settings.animDelay); 
    } 
    animate(); 
} 
+0

爲什麼不使用數組‘$。每個’?我需要能有新的生產線(
),所以我需要使用,而不是「的.text」右「的.html」? –