The scripts on this question were working great for me, until I had numbers that needed to include thousands separators. Now I get an error "NAN" for those. I need the separators to be there. Not sure how to solve this. The only (hacky) thing I can think of is to just have counts for every group between the separators, so for 1,160,868, it would be like
<span class="count">1</span>,<span class="count">160</span>,<span class="count">868</span>
// Counter animation
// inViewport jQuery plugin
// http://stackoverflow.com/a/26831113/383904
$(function($, win) {
$.fn.inViewport = function(cb) {
return this.each(function(i,el){
function visPx(){
var H = $(this).height(),
r = el.getBoundingClientRect(), t=r.top, b=r.bottom;
return cb.call(el, Math.max(0, t>0? H-t : (b<H?b:H)));
} visPx();
$(win).on("resize scroll", visPx);
});
};
}(jQuery, window));
jQuery(function($) { // DOM ready and $ in scope
$(".count").inViewport(function(px) { // Make use of the `px` argument!!!
// if element entered V.port ( px>0 ) and
// if prop initNumAnim flag is not yet set
// = Animate numbers
if(px>0 && !this.initNumAnim) {
this.initNumAnim = true; // Set flag to true to prevent re-running the same animation
$(this).prop('Counter',0).animate({
Counter: $(this).text()
}, {
duration: 4000,
step: function (now) {
$(this).text(Math.ceil(now));
}
});
}
});
});
// end Counter animation
Which would work, technically. And if that's the way I need to do it, I will. But it would be a whole lot easier for my backend team, when they're pulling these numbers from the database, to be able to just grab the number with the comma separators.