jQuery animated number counter from zero to value

后端 未结 10 712
天涯浪人
天涯浪人 2020-11-28 04:00

I have created a script to animate a number from zero to it\'s value.

Working

jQuery

10条回答
  •  -上瘾入骨i
    2020-11-28 04:21

    IMPORTANT: It seems like a small difference but you should really use a data attribute to hold the original number to count up to. Altering the original number can have un-intended consequences. For instance, I'm having this animation run everytime an element enters the screen. But if the element enters, exits, and then enters the screen a second time before the first animation finishes, it will count up to the wrong number.

    HTML:

    200

    70

    32

    JQuery:

    $('.count').each(function () {
        $(this).prop('Counter', 0).animate({
                Counter: $(this).data('value')
            }, {
            duration: 1000,
            easing: 'swing',
            step: function (now) {                      
                $(this).text(this.Counter.toFixed(2));
            }
        });
    });
    

提交回复
热议问题