开发者

Adding text character by character using .animate() jQuery

I am using jQuery and AJAX on my website. My AJAX respond is a text representing the continuance of an article. I've read that there's a STEP function in jQuery's 开发者_开发问答animate but I don't know how to use that to append text character by character to a DIV element. Please Help me.

Thanks in advance. :D


Try this.

var someajaxtext = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras interdum sem sit amet magna convallis sed ullamcorper mi commodo.';
$('button').click(function () {
    var dv = $('#mydiv');
    dv.text("");
    jQuery({
        count: 0
    }).animate({
        count: someajaxtext.length
    }, {
        duration: 1500,
        step: function () {
            dv.text(someajaxtext.substring(0, Math.round(this.count)));
        }
    });
});

Demo here: http://jsfiddle.net/naveen/evVMw/

P.S: Change the duration as you wish.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜