$.fn.countto = function(a) { a = a || {}; return $(this).each(function() { var c = $.extend({}, $.fn.countto.defaults, { from: $(this).data("from"), to: $(this).data("to"), speed: $(this).data("speed"), refreshinterval: $(this).data("refresh-interval"), decimals: $(this).data("decimals") }, a); var h = math.ceil(c.speed / c.refreshinterval), i = (c.to - c.from) / h; var j = this, f = $(this), e = 0, g = c.from, d = f.data("countto") || {}; f.data("countto", d); if (d.interval) { clearinterval(d.interval) } d.interval = setinterval(k, c.refreshinterval); b(g); function k() { g += i; e++; b(g); if (typeof(c.onupdate) == "function") { c.onupdate.call(j, g) } if (e >= h) { f.removedata("countto"); clearinterval(d.interval); g = c.to; if (typeof(c.oncomplete) == "function") { c.oncomplete.call(j, g) } } } function b(m) { var l = c.formatter.call(j, m, c); f.html(l) } }) }; $.fn.countto.defaults = { from: 0, to: 0, speed: 1000, refreshinterval: 100, decimals: 0, formatter: formatter, onupdate: null, oncomplete: null }; function formatter(b, a) { return b.tofixed() } //整数 $(".count-number").data("counttooptions", { formatter: function(b, a) { return b.tofixed().replace(/\b(?=(?:\d{3})+(?!\d))/g, ",") } }); //两位小数 $(".count-number2").data("counttooptions", { formatter: function(b, a) { return b.tofixed(2).replace(/\b(?=(?:\d{3})+(?!\d))/g, ",") } }); //一位小数 $(".count-number3").data("counttooptions", { formatter: function(b, a) { return b.tofixed(1).replace(/\b(?=(?:\d{3})+(?!\d))/g, ",") } });