From 62cf38a102a9c6a11d89f3957bd001548dbf8852 Mon Sep 17 00:00:00 2001 From: Nader Abu Fakhr Date: Sun, 14 Dec 2014 00:03:16 -0430 Subject: [PATCH] Added floating numbers support --- README.md | 2 +- build/angular-count-to.min.js | 2 +- src/count-to.js | 13 +++++++------ 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 38ba862..0b6927e 100644 --- a/README.md +++ b/README.md @@ -31,4 +31,4 @@ The following attributes can be set as numbers on the directive element. - ```count-to``` the number to count to. - ```value``` the number to start counting from. - ```duration``` how long the count should take in seconds. - +- ```precision``` number of decimals if count-to is a floating number. diff --git a/build/angular-count-to.min.js b/build/angular-count-to.min.js index 91e224e..24c889e 100644 --- a/build/angular-count-to.min.js +++ b/build/angular-count-to.min.js @@ -1,2 +1,2 @@ /*! angular-count-to 2013-07-16 */ -var countTo=angular.module("countTo",[]).directive("countTo",["$timeout",function(a){return{replace:!1,scope:!0,link:function(b,c,d){var e,f,g,h,i,j,k,l=c[0],m=function(){f=30,i=0,b.timoutId=null,j=parseInt(d.countTo)||0,b.value=parseInt(d.value,10)||0,g=1e3*parseFloat(d.duration)||0,h=Math.ceil(g/f),k=(j-b.value)/h,e=b.value},n=function(){b.timoutId=a(function(){e+=k,i++,i>=h?(a.cancel(b.timoutId),e=j,l.innerText=j):(l.innerText=Math.round(e),n())},f)},o=function(){b.timoutId&&a.cancel(b.timoutId),m(),n()};return d.$observe("countTo",function(a){a&&o()}),d.$observe("value",function(){o()}),!0}}}]); \ No newline at end of file +var countTo=angular.module("countTo",[]).directive("countTo",["$timeout",function(e){return{replace:false,scope:true,link:function(t,n,r){var i=n[0];var s,o,u,a,f,l,c,h,p;var d=function(){o=30;f=0;t.timoutId=null;p=parseInt(r.precision)||0;l=parseFloat(r.countTo)||0;t.value=parseFloat(r.value,10)||0;u=parseFloat(r.duration)*1e3||0;a=u/o;h=(l-t.value)/a;s=t.value};var v=function(){t.timoutId=e(function(){s+=h;f++;if(f>=a){e.cancel(t.timoutId);s=l;i.textContent=l.toFixed(p)}else{i.textContent=s.toFixed(p);v()}},o)};var m=function(){if(t.timoutId){e.cancel(t.timoutId)}d();v()};r.$observe("countTo",function(e){if(e){m()}});r.$observe("value",function(e){m()});return true}}}]) \ No newline at end of file diff --git a/src/count-to.js b/src/count-to.js index d3aa7ad..4d8700a 100644 --- a/src/count-to.js +++ b/src/count-to.js @@ -6,17 +6,18 @@ var countTo = angular.module('countTo', []) link: function (scope, element, attrs) { var e = element[0]; - var num, refreshInterval, duration, steps, step, countTo, value, increment; + var num, refreshInterval, duration, steps, step, countTo, value, increment, precision; var calculate = function () { refreshInterval = 30; step = 0; scope.timoutId = null; - countTo = parseInt(attrs.countTo) || 0; - scope.value = parseInt(attrs.value, 10) || 0; + precision = parseInt(attrs.precision) || 0; + countTo = parseFloat(attrs.countTo) || 0; + scope.value = parseFloat(attrs.value, 10) || 0; duration = (parseFloat(attrs.duration) * 1000) || 0; - steps = Math.ceil(duration / refreshInterval); + steps = duration / refreshInterval; increment = ((countTo - scope.value) / steps); num = scope.value; } @@ -28,9 +29,9 @@ var countTo = angular.module('countTo', []) if (step >= steps) { $timeout.cancel(scope.timoutId); num = countTo; - e.textContent = countTo; + e.textContent = countTo.toFixed(precision); } else { - e.textContent = Math.round(num); + e.textContent = num.toFixed(precision); tick(); } }, refreshInterval);