Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery Plugin for animating numbers

I am making an ajax call to the server and then updating some stats. I want a plugin which animates the numbers.

e.g. initial value = 65 value after ajax call = 98

in a span of 2 seconds, the value displayed increases from 65 to 98 and the user is able to see that - like the digital speedometers or tachometers.

My search has not led me to find a plugin for this. Does anybody know of such plugin?

like image 733
shashi Avatar asked Feb 17 '11 21:02

shashi


People also ask

Is jQuery good for animation?

The jQuery library provides several techniques for adding animation to a web page. These include simple, standard animations that are frequently used, and the ability to craft sophisticated custom effects.

How do you animate in jQuery?

The jQuery animate() method is used to create custom animations. Syntax: $(selector).animate({params},speed,callback); The required params parameter defines the CSS properties to be animated.

Which jQuery function is used to add animation to an element?

The . animate() method allows us to create animation effects on any numeric CSS property. The only required parameter is a plain object of CSS properties.


4 Answers

Here's one potential solution using jQuery's animate(), implemented as a function. The "duration" and "easing" arguments are optional.

function animateNumber($input, num, duration, easing)
{
    $input
        .data("start", parseInt($input.val()))
        .animate({"val":parseInt(num)},
        {
            easing: easing == undefined ? "linear" : easing,
            duration: duration == undefined ? 500 : parseInt(duration),
            step: function(fin,obj)
            {
                var $this = jQuery(this);
                var start = parseInt($this.data("start"));
                $this.val(parseInt((fin-start)*obj.state) + start ); 
            }
        });
}

// Usage
animateNumber($("#my_input_box"), 23);
animateNumber($("#my_input_box"), 23, 1500, "swing"); // 1.5 sec, swing easing
like image 141
Luke Dennis Avatar answered Nov 15 '22 06:11

Luke Dennis


It doesnt have a duration, but it's kinda close. I'm not sure how to integrate a duration at the moment, and had to throw this together rather quickly.

(function($) {
    $.fn.animateNumber = function(to) {
        var $ele = $(this),
            num = parseInt($ele.html()),
            up = to > num,
            num_interval = Math.abs(num - to) / 90;

        var loop = function() {
            num = Math.floor(up ? num+num_interval: num-num_interval);
            if ( (up && num > to) || (!up && num < to) ) {
                num = to;
                clearInterval(animation)
            }
            $ele.html(num);
        }

        var animation = setInterval(loop, 5);
    }
})(jQuery)

var $counter = $("#counter");
$counter.animateNumber(800);
<span id="counter">100</span>
like image 37
simshaun Avatar answered Nov 15 '22 05:11

simshaun


Simshaun's code works perfectly, except in the case where the number to animate to is less than the current value; leading to an infinite loop of numeric flooring.

Here's the updated code,

(function($) {
    $.fn.animateNumber = function(to) {
        var $ele = $(this),
            num = parseInt($ele.html()),
            up = to > num,
            num_interval = Math.abs(num - to) / 90;

        var loop = function() {
            num = up ? Math.ceil(num+num_interval) : Math.floor(num-num_interval);
            if ( (up && num > to) || (!up && num < to) ) {
                num = to;
                clearInterval(animation)
            }
            $ele.html(num);
        }

        var animation = setInterval(loop, 5);
    }
})(jQuery)

var $counter = $("#counter");
$counter.animateNumber(800);
<span id="counter">100</span>
like image 38
Amit G Avatar answered Nov 15 '22 07:11

Amit G


Here is a jquery plugin that allows you to animate numbers:

https://github.com/kajic/jquery-animateNumber

like image 37
Robert Kajic Avatar answered Nov 15 '22 05:11

Robert Kajic