From 7c04f135b4295f523208ff8a65253f3da2c58bcb Mon Sep 17 00:00:00 2001 From: John Dimas Date: Sat, 1 Feb 2020 22:08:40 +0200 Subject: [PATCH] Added two options to counterUp. First one is a fix for european number formatting. Second one is to destroy the waypoint (in case you don't want the animation to be repeated at each waypoint pass). --- jquery.counterup.js | 22 +++++++++++++++++++--- jquery.counterup.min.js | 6 +++++- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/jquery.counterup.js b/jquery.counterup.js index bc7871f..077f270 100644 --- a/jquery.counterup.js +++ b/jquery.counterup.js @@ -14,7 +14,9 @@ // Defaults var settings = $.extend({ 'time': 400, - 'delay': 10 + 'delay': 10, + 'european': false, + 'repeat': true // switch to false to destroy waypoint }, options); return this.each(function(){ @@ -32,6 +34,7 @@ var isInt = /^[0-9]+$/.test(num); var isFloat = /^[0-9]+\.[0-9]+$/.test(num); var decimalPlaces = isFloat ? (num.split('.')[1] || []).length : 0; + var delimeter = ','; // Generate list of incremental numbers to display for (var i = divisions; i >= 1; i--) { @@ -44,10 +47,17 @@ newNum = parseFloat(num / divisions * i).toFixed(decimalPlaces); } + // Change number to european format + if ($settings.european) { + delimeter = '.'; + newNum = newNum.toString().replace(/\./, ','); + } + // Preserve commas if input had commas if (isComma) { while (/(\d+)(\d{3})/.test(newNum.toString())) { - newNum = newNum.toString().replace(/(\d+)(\d{3})/, '$1'+','+'$2'); + // ref: https://blog.abelotech.com/posts/number-currency-formatting-javascript/ + newNum = newNum.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g, '$1' + delimeter); } } @@ -75,7 +85,13 @@ }; // Perform counts when the element gets into view - $this.waypoint(counterUpper, { offset: '100%', triggerOnce: true }); + $this.waypoint(function(){ + counterUpper(); + + if (!$settings.repeat) { + this.destroy(); + } + }, { offset: '100%', triggerOnce: true }); }); }; diff --git a/jquery.counterup.min.js b/jquery.counterup.min.js index cddf5a1..ed6d5a3 100644 --- a/jquery.counterup.min.js +++ b/jquery.counterup.min.js @@ -5,4 +5,8 @@ * 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($){"use strict";$.fn.counterUp=function(options){var settings=$.extend({'time':400,'delay':10,'european':!1,'repeat':!0},options);return this.each(function(){var $this=$(this);var $settings=settings;var counterUpper=function(){var nums=[];var divisions=$settings.time/$settings.delay;var num=$this.text();var isComma=/[0-9]+,[0-9]+/.test(num);num=num.replace(/,/g,'');var isInt=/^[0-9]+$/.test(num);var isFloat=/^[0-9]+\.[0-9]+$/.test(num);var decimalPlaces=isFloat?(num.split('.')[1]||[]).length:0;var delimeter=',';for(var i=divisions;i>=1;i--){var newNum=parseInt(num/divisions*i);if(isFloat){newNum=parseFloat(num/divisions*i).toFixed(decimalPlaces)} +if($settings.european){delimeter='.';newNum=newNum.toString().replace(/\./,',')} +if(isComma){while(/(\d+)(\d{3})/.test(newNum.toString())){newNum=newNum.toString().replace(/(\d)(?=(\d{3})+(?!\d))/g,'$1'+delimeter)}} +nums.unshift(newNum)} +$this.data('counterup-nums',nums);$this.text('0');var f=function(){$this.text($this.data('counterup-nums').shift());if($this.data('counterup-nums').length){setTimeout($this.data('counterup-func'),$settings.delay)}else{delete $this.data('counterup-nums');$this.data('counterup-nums',null);$this.data('counterup-func',null)}};$this.data('counterup-func',f);setTimeout($this.data('counterup-func'),$settings.delay)};$this.waypoint(function(){counterUpper();if(!$settings.repeat){this.destroy()}},{offset:'100%',triggerOnce:!0})})}})(jQuery) \ No newline at end of file