From ff9dd6ae26b79017f1b631956f93a32022e1c219 Mon Sep 17 00:00:00 2001 From: tobias-forkel Date: Fri, 5 Feb 2016 13:15:24 -0500 Subject: [PATCH] Start counter with a different duration and delay by setting data-counterup-time="" and data-counterup-delay="" --- README.md | 6 ++++++ counterup.jquery.json | 2 +- demo/demo.html | 10 ++++++---- jquery.counterup.js | 18 +++++++++++------- jquery.counterup.min.js | 6 +++--- 5 files changed, 27 insertions(+), 15 deletions(-) diff --git a/README.md b/README.md index 6a17d40..b56c1c4 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,7 @@ Features: * Auto-detect for integers, floats or formatted numbers * The plugin will also use the number of decimal places the original number is using. +* Start counter with a different duration and delay by setting `data-counterup-time=""` and `data-counterup-delay=""` * Lightweight: ~1kb * Minimal setup @@ -33,11 +34,16 @@ Usage **HTML** +With default values from plugin instantiation. ``` 1,234,567.00 $1.99 12345 ``` +With values from `data` attribute. +``` +1,234,567.00 +``` **jQuery** diff --git a/counterup.jquery.json b/counterup.jquery.json index 89bf645..5018abd 100644 --- a/counterup.jquery.json +++ b/counterup.jquery.json @@ -1,6 +1,6 @@ { "name": "counterup", - "version": "1.0.0", + "version": "1.1.0", "title": "Counter-Up", "description": "A lightweight jQuery plugin that counts up to a targeted number when the number becomes visible.", "keywords": [ diff --git a/demo/demo.html b/demo/demo.html index dc32556..8c2b0d9 100644 --- a/demo/demo.html +++ b/demo/demo.html @@ -1,10 +1,7 @@ - - - Counter Up Demo @@ -33,7 +30,7 @@ span { font-size: 66px; color: #555; - margin-bottom: 350px; + margin-bottom: 250px; display: inline-block; font-weight: 400; text-align: center; @@ -129,6 +126,11 @@

(Scroll down)

1.9583 12345 +
+ 52,147 + 1.9583 + 12345 +
$43,753 1,734,195.10 diff --git a/jquery.counterup.js b/jquery.counterup.js index bc7871f..dfda770 100644 --- a/jquery.counterup.js +++ b/jquery.counterup.js @@ -1,10 +1,10 @@ /*! -* jquery.counterup.js 1.0 +* jquery.counterup.js 1.1.0 * * Copyright 2013, Benjamin Intal http://gambit.ph @bfintal * Released under the GPL v2 License * -* Date: Nov 26, 2013 +* Date: Feb 05, 2016 */ (function( $ ){ "use strict"; @@ -21,11 +21,15 @@ // Store the object var $this = $(this); - var $settings = settings; + + var counter = { + time: $(this).data('counterup-time') || settings.time, + delay: $(this).data('counterup-delay') || settings.delay + }; var counterUpper = function() { var nums = []; - var divisions = $settings.time / $settings.delay; + var divisions = counter.time / counter.delay; var num = $this.text(); var isComma = /[0-9]+,[0-9]+/.test(num); num = num.replace(/,/g, ''); @@ -61,7 +65,7 @@ var f = function() { $this.text($this.data('counterup-nums').shift()); if ($this.data('counterup-nums').length) { - setTimeout($this.data('counterup-func'), $settings.delay); + setTimeout($this.data('counterup-func'), counter.delay); } else { delete $this.data('counterup-nums'); $this.data('counterup-nums', null); @@ -71,7 +75,7 @@ $this.data('counterup-func', f); // Start the count up - setTimeout($this.data('counterup-func'), $settings.delay); + setTimeout($this.data('counterup-func'), counter.delay); }; // Perform counts when the element gets into view @@ -80,4 +84,4 @@ }; -})( jQuery ); \ No newline at end of file +})( jQuery ); diff --git a/jquery.counterup.min.js b/jquery.counterup.min.js index cddf5a1..16b3a0a 100644 --- a/jquery.counterup.min.js +++ b/jquery.counterup.min.js @@ -1,8 +1,8 @@ /*! -* jquery.counterup.js 1.0 +* jquery.counterup.js 1.1.0 * * Copyright 2013, Benjamin Intal http://gambit.ph @bfintal * 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 +* Date: Feb 05, 2016 +*/!function(t){"use strict";t.fn.counterUp=function(e){var u=t.extend({time:400,delay:10},e);return this.each(function(){var e=t(this),n={time:t(this).data("counterup-time")||u.time,delay:t(this).data("counterup-delay")||u.delay},a=function(){var t=[],u=n.time/n.delay,a=e.text(),r=/[0-9]+,[0-9]+/.test(a);a=a.replace(/,/g,"");for(var o=(/^[0-9]+$/.test(a),/^[0-9]+\.[0-9]+$/.test(a)),c=o?(a.split(".")[1]||[]).length:0,i=u;i>=1;i--){var d=parseInt(a/u*i);if(o&&(d=parseFloat(a/u*i).toFixed(c)),r)for(;/(\d+)(\d{3})/.test(d.toString());)d=d.toString().replace(/(\d+)(\d{3})/,"$1,$2");t.unshift(d)}e.data("counterup-nums",t),e.text("0");var s=function(){e.text(e.data("counterup-nums").shift()),e.data("counterup-nums").length?setTimeout(e.data("counterup-func"),n.delay):(delete e.data("counterup-nums"),e.data("counterup-nums",null),e.data("counterup-func",null))};e.data("counterup-func",s),setTimeout(e.data("counterup-func"),n.delay)};e.waypoint(a,{offset:"100%",triggerOnce:!0})})}}(jQuery); \ No newline at end of file