Commit 5f3a0656 authored by Philipp Müller's avatar Philipp Müller
Browse files

[TASK] Added number format

parent 37b09bf8
......@@ -41,7 +41,7 @@ let lavitto_counter = {
if (!el.hasClass('counter-started')) {
let delay = parseFloat(el.data('counter-delay')) * 1000;
setTimeout(function () {
if(el.data('counter-start-effect') === 'fadein') {
if (el.data('counter-start-effect') === 'fadein') {
let startEffectDuration = parseInt(el.data('counter-start-effect-duration'));
el.animate({'opacity': 1}, startEffectDuration);
}
......@@ -67,14 +67,54 @@ let lavitto_counter = {
duration: duration,
easing: easing,
step: function () {
numberEl.text(Math.ceil(this.count));
numberEl.text(lavitto_counter.numberFormat(Math.ceil(this.count)));
},
complete: function () {
numberEl.text(end);
numberEl.text(lavitto_counter.numberFormat(end));
}
});
},
/**
* Returns the number as formatted string
*
* @param number
* @returns {string}
*/
numberFormat: function (number) {
number = (number + '').replace(/[^0-9+\-Ee.]/g, '');
let decimals = 0, decPoint = '.', thousandsSep = '\'';
let n = !isFinite(+number) ? 0 : +number,
prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
sep = (typeof thousandsSep === 'undefined') ? ',' : thousandsSep,
dec = (typeof decPoint === 'undefined') ? '.' : decPoint,
s = '';
let toFixedFix = function (n, prec) {
if (('' + n).indexOf('e') === -1) {
return +(Math.round(n + 'e+' + prec) + 'e-' + prec)
} else {
let arr = ('' + n).split('e'),
sig = '';
if (+arr[1] + prec > 0) {
sig = '+'
}
return (+(Math.round(+arr[0] + 'e' + sig + (+arr[1] + prec)) + 'e-' + prec)).toFixed(prec)
}
};
s = (prec ? toFixedFix(n, prec).toString() : '' + Math.round(n)).split('.');
if (s[0].length > 3) {
s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep)
}
if ((s[1] || '').length < prec) {
s[1] = s[1] || ''
s[1] += new Array(prec - s[1].length + 1).join('0')
}
return s.join(dec)
},
/**
* Checks if an element is in the viewport
*
......
let lavitto_counter={init:function(){$(".ce-counter").each(function(){let el=$(this);let lazyStart=parseInt(el.data("counter-lazy-start"));if(lazyStart===1){$(window).on("resize scroll",function(){if(lavitto_counter.isInViewport(el)===true){lavitto_counter.startCounterWithDelay(el)}});if(lavitto_counter.isInViewport(el)===true){lavitto_counter.startCounterWithDelay(el)}}else{lavitto_counter.startCounterWithDelay(el)}})},startCounterWithDelay:function(a){if(!a.hasClass("counter-started")){let delay=parseFloat(a.data("counter-delay"))*1000;setTimeout(function(){if(a.data("counter-start-effect")==="fadein"){let startEffectDuration=parseInt(a.data("counter-start-effect-duration"));a.animate({opacity:1},startEffectDuration)}lavitto_counter.startCounter(a)},delay)}},startCounter:function(a){a.addClass("counter-started");let start=parseInt(a.data("counter-start")),end=parseInt(a.data("counter-end")),duration=parseFloat(a.data("counter-duration"))*1000,easing=a.data("counter-easing");let numberEl=a.find(".number");$({count:start}).animate({count:end},{duration:duration,easing:easing,step:function(){numberEl.text(Math.ceil(this.count))},complete:function(){numberEl.text(end)}})},isInViewport:function(a){let elementTop=a.offset().top;let elementBottom=elementTop+a.outerHeight();let viewportTop=$(window).scrollTop();let viewportBottom=viewportTop+$(window).height();return elementBottom>=viewportTop&&elementTop<=viewportBottom}};$(document).ready(function(){lavitto_counter.init()});
\ No newline at end of file
let lavitto_counter={init:function(){$(".ce-counter").each(function(){let el=$(this);let lazyStart=parseInt(el.data("counter-lazy-start"));if(lazyStart===1){$(window).on("resize scroll",function(){if(lavitto_counter.isInViewport(el)===true){lavitto_counter.startCounterWithDelay(el)}});if(lavitto_counter.isInViewport(el)===true){lavitto_counter.startCounterWithDelay(el)}}else{lavitto_counter.startCounterWithDelay(el)}})},startCounterWithDelay:function(a){if(!a.hasClass("counter-started")){let delay=parseFloat(a.data("counter-delay"))*1000;setTimeout(function(){if(a.data("counter-start-effect")==="fadein"){let startEffectDuration=parseInt(a.data("counter-start-effect-duration"));a.animate({opacity:1},startEffectDuration)}lavitto_counter.startCounter(a)},delay)}},startCounter:function(a){a.addClass("counter-started");let start=parseInt(a.data("counter-start")),end=parseInt(a.data("counter-end")),duration=parseFloat(a.data("counter-duration"))*1000,easing=a.data("counter-easing");let numberEl=a.find(".number");$({count:start}).animate({count:end},{duration:duration,easing:easing,step:function(){numberEl.text(lavitto_counter.numberFormat(Math.ceil(this.count)))},complete:function(){numberEl.text(lavitto_counter.numberFormat(end))}})},numberFormat:function(a){a=(a+"").replace(/[^0-9+\-Ee.]/g,"");let decimals=0,decPoint=".",thousandsSep="'";let n=!isFinite(+a)?0:+a,prec=!isFinite(+decimals)?0:Math.abs(decimals),sep=(typeof thousandsSep==="undefined")?",":thousandsSep,dec=(typeof decPoint==="undefined")?".":decPoint,s="";let toFixedFix=function(c,b){if((""+c).indexOf("e")===-1){return +(Math.round(c+"e+"+b)+"e-"+b)}else{let arr=(""+c).split("e"),sig="";if(+arr[1]+b>0){sig="+"}return(+(Math.round(+arr[0]+"e"+sig+(+arr[1]+b))+"e-"+b)).toFixed(b)}};s=(prec?toFixedFix(n,prec).toString():""+Math.round(n)).split(".");if(s[0].length>3){s[0]=s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g,sep)}if((s[1]||"").length<prec){s[1]=s[1]||"";s[1]+=new Array(prec-s[1].length+1).join("0")}return s.join(dec)},isInViewport:function(a){let elementTop=a.offset().top;let elementBottom=elementTop+a.outerHeight();let viewportTop=$(window).scrollTop();let viewportBottom=viewportTop+$(window).height();return elementBottom>=viewportTop&&elementTop<=viewportBottom}};$(document).ready(function(){lavitto_counter.init()});
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment