From 05b068007dee900a5b96340e6e1b2d41b70b060f Mon Sep 17 00:00:00 2001 From: Josh Lee Date: Wed, 25 Jan 2017 20:46:30 +0000 Subject: [PATCH 1/2] Added functionality to specify the number to begin moving up from --- README.md | 5 ++++- demo/demo.html | 2 +- jquery.counterup.js | 11 ++++++----- jquery.counterup.min.js | 14 +++++++++++++- 4 files changed, 24 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 6a17d40..eeab018 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,8 @@ $('.counter').counterUp(); ``` $('.counter').counterUp({ delay: 10, - time: 1000 + time: 1000, + beginAt: 0 }); ``` @@ -58,6 +59,8 @@ $('.counter').counterUp({ `time` - The total duration of the count up animation +`beginAt` - Where you want the number to begin moving up from + Social Stuff ============ diff --git a/demo/demo.html b/demo/demo.html index dc32556..dc0ad40 100644 --- a/demo/demo.html +++ b/demo/demo.html @@ -157,4 +157,4 @@

(Scroll down)

- \ No newline at end of file + diff --git a/jquery.counterup.js b/jquery.counterup.js index bc7871f..20fc737 100644 --- a/jquery.counterup.js +++ b/jquery.counterup.js @@ -14,7 +14,8 @@ // Defaults var settings = $.extend({ 'time': 400, - 'delay': 10 + 'delay': 10, + 'beginAt': 1 }, options); return this.each(function(){ @@ -32,10 +33,10 @@ var isInt = /^[0-9]+$/.test(num); var isFloat = /^[0-9]+\.[0-9]+$/.test(num); var decimalPlaces = isFloat ? (num.split('.')[1] || []).length : 0; + if ($settings.beginAt > num) $settings.beginAt = num // Generate list of incremental numbers to display - for (var i = divisions; i >= 1; i--) { - + for (var i = divisions; i >= $settings.beginAt / num * divisions; i--) { // Preserve as int if input was int var newNum = parseInt(num / divisions * i); @@ -55,7 +56,7 @@ } $this.data('counterup-nums', nums); - $this.text('0'); + $this.text($settings.beginAt); // Updates the number until we're done var f = function() { @@ -80,4 +81,4 @@ }; -})( jQuery ); \ No newline at end of file +})( jQuery ); diff --git a/jquery.counterup.min.js b/jquery.counterup.min.js index cddf5a1..41afb3f 100644 --- a/jquery.counterup.min.js +++ b/jquery.counterup.min.js @@ -5,4 +5,16 @@ * Released under the GPL v2 License * * Date: Nov 26, 2013 -*/(function(e){"use strict";e.fn.counterUp=function(t){var n=e.extend({time:400,delay:10},t);return this.each(function(){var t=e(this),r=n,i=function(){var e=[],n=r.time/r.delay,i=t.text(),s=/[0-9]+,[0-9]+/.test(i);i=i.replace(/,/g,"");var o=/^[0-9]+$/.test(i),u=/^[0-9]+\.[0-9]+$/.test(i),a=u?(i.split(".")[1]||[]).length:0;for(var f=n;f>=1;f--){var l=parseInt(i/n*f);u&&(l=parseFloat(i/n*f).toFixed(a));if(s)while(/(\d+)(\d{3})/.test(l.toString()))l=l.toString().replace(/(\d+)(\d{3})/,"$1,$2");e.unshift(l)}t.data("counterup-nums",e);t.text("0");var c=function(){t.text(t.data("counterup-nums").shift());if(t.data("counterup-nums").length)setTimeout(t.data("counterup-func"),r.delay);else{delete t.data("counterup-nums");t.data("counterup-nums",null);t.data("counterup-func",null)}};t.data("counterup-func",c);setTimeout(t.data("counterup-func"),r.delay)};t.waypoint(i,{offset:"100%",triggerOnce:!0})})}})(jQuery); \ No newline at end of file +*/ +!function(t){"use strict" +t.fn.counterUp=function(e){var n=t.extend({time:400,delay:10,beginAt:1},e) +return this.each(function(){var e=t(this),u=n,a=function(){var t=[],n=u.time/u.delay,a=e.text(),r=/[0-9]+,[0-9]+/.test(a) +a=a.replace(/,/g,"") +var i=(/^[0-9]+$/.test(a),/^[0-9]+\.[0-9]+$/.test(a)),o=i?(a.split(".")[1]||[]).length:0 +u.beginAt>a&&(u.beginAt=a) +for(var c=n;c>=u.beginAt/a*n;c--){var s=parseInt(a/n*c) +if(i&&(s=parseFloat(a/n*c).toFixed(o)),r)for(;/(\d+)(\d{3})/.test(s.toString());)s=s.toString().replace(/(\d+)(\d{3})/,"$1,$2") +t.unshift(s)}e.data("counterup-nums",t),e.text(u.beginAt) +var d=function(){e.text(e.data("counterup-nums").shift()),e.data("counterup-nums").length?setTimeout(e.data("counterup-func"),u.delay):(delete e.data("counterup-nums"),e.data("counterup-nums",null),e.data("counterup-func",null))} +e.data("counterup-func",d),setTimeout(e.data("counterup-func"),u.delay)} +e.waypoint(a,{offset:"100%",triggerOnce:!0})})}}(jQuery) From e61066cc88d385dcf09a1ae897cc4bc8322bb9cf Mon Sep 17 00:00:00 2001 From: Josh Lee Date: Sun, 19 Feb 2017 01:01:38 +0000 Subject: [PATCH 2/2] Change beginAt default to 0 instead of 1 --- jquery.counterup.js | 2 +- jquery.counterup.min.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/jquery.counterup.js b/jquery.counterup.js index 20fc737..3128b65 100644 --- a/jquery.counterup.js +++ b/jquery.counterup.js @@ -15,7 +15,7 @@ var settings = $.extend({ 'time': 400, 'delay': 10, - 'beginAt': 1 + 'beginAt': 0 }, options); return this.each(function(){ diff --git a/jquery.counterup.min.js b/jquery.counterup.min.js index 41afb3f..a3a842f 100644 --- a/jquery.counterup.min.js +++ b/jquery.counterup.min.js @@ -7,7 +7,7 @@ * Date: Nov 26, 2013 */ !function(t){"use strict" -t.fn.counterUp=function(e){var n=t.extend({time:400,delay:10,beginAt:1},e) +t.fn.counterUp=function(e){var n=t.extend({time:400,delay:10,beginAt:0},e) return this.each(function(){var e=t(this),u=n,a=function(){var t=[],n=u.time/u.delay,a=e.text(),r=/[0-9]+,[0-9]+/.test(a) a=a.replace(/,/g,"") var i=(/^[0-9]+$/.test(a),/^[0-9]+\.[0-9]+$/.test(a)),o=i?(a.split(".")[1]||[]).length:0