Display a sentence, one character at a time

后端 未结 6 2087
佛祖请我去吃肉
佛祖请我去吃肉 2021-02-10 03:00

I want to display a sentence one character at a time, one by one, with jQuery. Is there a plugin that can do this Or how could I get this effect?

6条回答
  •  不思量自难忘°
    2021-02-10 03:34

    You could write a little plugin to do it. Here's something to get you started (far from perfect, just to give you an idea!):

    (function($) {
        $.fn.writeText = function(content) {
            var contentArray = content.split(""),
                current = 0,
                elem = this;
            setInterval(function() {
                if(current < contentArray.length) {
                    elem.text(elem.text() + contentArray[current++]);
                }
            }, 100);
        };
    
    })(jQuery);
    

    You can call that like this:

    $("#element").writeText("This is some text");
    

    Here's a working example.

提交回复
热议问题