Skip to content
This repository has been archived by the owner on Aug 22, 2021. It is now read-only.

European number format and trigger once options #72

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 19 additions & 3 deletions jquery.counterup.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(){
Expand All @@ -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--) {
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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 });
});

};
Expand Down
6 changes: 5 additions & 1 deletion jquery.counterup.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.