JSFiddle - React, Tailwind, and code Playground
by Simon Hi
HTML
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/waypoints/4.0.1/jquery.waypoints.js"></script>
<span class='number counterPlugin'>150 000</span>
<span class='number counterPlugin'>150</span>
<span class='number counterPlugin'>1 000</span>
<span class='number counterPlugin'>50</span>
CSS
.number{
margin: 1000px 0;
display: block;
}
JavaScript
/*!
* jquery.counterup.js 1.0
*
* Copyright 2013, Benjamin Intal http://gambit.ph @bfintal
* Released under the GPL v2 License
* Modified by parsimonhi
*/
(function(e) {
"use strict";
function isVisible(e) {
var r = e.getBoundingClientRect();
var h = Math.max(document.documentElement.clientHeight, window.innerHeight);
return !((r.bottom < 0)||((r.top - h)>= 0));
}
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 = i=t.text().replace(/,/,".").replace(/\s/g,","),
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() {
if(!t.data("started")&&isVisible(t[0]))
t.data("started",true);
if(t.data("started"))
t.text((t.data("counterup-nums").shift()+"").replace(/,/g," ").replace(".",","));
if (t.data("counterup-nums").length) setTimeout(t.data("counterup-func"), r.delay);
else {
delete t.data("counterup-nums");
...