Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery animate decimal number increment/decrement

I would like to animate difference between two decimal numbers step by step.

Have found Joss Crowcroft's solution for integer numbers that works nice and I've made example on jsfiddle. Code snippet:

$({numberValue: 35}).animate({numberValue: 100}, {
    duration: 1000,
    easing: 'linear',
    step: function() { 
        $('#dynamic-number').text(Math.ceil(this.numberValue)); 
    }
});

But if I want to animate for example number 2.85 to 3.25, can't be done on ths way. There have to be animated both parts, integer and decimal. Can it be made on simplier way except separated animations for integers and decimals?

like image 885
swolfish Avatar asked May 15 '13 20:05

swolfish


2 Answers

You mean like this?

var currentNumber = $('#dynamic-number').text();

$({numberValue: currentNumber}).animate({numberValue: 100}, {
    duration: 8000,
    easing: 'linear',
    step: function() { 
        $('#dynamic-number').text(Math.ceil(this.numberValue*100)/100); 
    }
});
like image 124
Robert McKee Avatar answered Nov 05 '22 08:11

Robert McKee


Try this

var currentNumber = $('#dynamic-number').text();

$({numberValue: currentNumber}).animate({numberValue: 100}, {
duration: 8000,
easing: 'linear',
step: function (now) {
        $('#dynamic-number').text(now.toFixed(2)); 
}
});

Here's the Fiddle

like image 35
codereaper Avatar answered Nov 05 '22 07:11

codereaper