Created
March 19, 2012 15:59
-
-
Save cyakimov/2117219 to your computer and use it in GitHub Desktop.
Animate numbers jQuery
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//http://stackoverflow.com/questions/149055/how-can-i-format-numbers-as-money-in-javascript | |
Number.prototype.formatMoney = function(c, d, t){ | |
var n = this, c = isNaN(c = Math.abs(c)) ? 2 : c, d = d == undefined ? "," : d, t = t == undefined ? "." : t, s = n < 0 ? "-" : "", i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0; | |
return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : ""); | |
}; | |
function animateNumberChange(el,number,callback, interval, relax_interval) | |
{ | |
var currentVal = el.data("value"); | |
var endVal = number; | |
if(!callback) { callback = null; } | |
if(!interval) { interval = 2000; } | |
if(!relax_interval) { relax_interval = 5000; } | |
el.data("value",endVal); | |
if (currentVal === endVal){ | |
return !callback ? null : setTimeout(callback,relax_interval); | |
} | |
var handle = setInterval(function () | |
{ | |
if (currentVal === endVal) | |
{ | |
clearInterval(handle); | |
!callback ? null : setTimeout(callback,interval); | |
} | |
else | |
{ | |
if(currentVal < endVal){ | |
currentVal++; | |
} else { | |
currentVal--; | |
} | |
el.text(currentVal.formatMoney(0, '.', ',')); | |
} | |
}, 1); | |
} | |
//HOW TO USE IT? | |
//http://jsfiddle.net/peghc/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment