// ------------------------------------------------------------------------------------------- // big number animation shortcode javascript // ------------------------------------------------------------------------------------------- (function($) { // options.simple_up = dont prepend leading zeros, options.instant_start = trigger counting instantly, options.start_timer = delay when to start counting $.fn.avia_sc_animated_number = function(options) { if(!this.length) return; if(this.is('.avia_sc_animated_number_active')) return; this.addclass('avia_sc_animated_number_active'); var simple_upcount = (options && options.simple_up) ? true : false, start_timer = (options && options.start_timer) ? options.start_timer : 300, format_number = function( number, number_format, final_number ) { var prepend = '', addzeros = final_number.tostring().length - number.tostring().length; //if the number has less digits than the final number some zeros where omitted. add them to the front for(var i = addzeros; i > 0; i--) { prepend += '0'; } number = ( simple_upcount ) ? number.tostring() : prepend + number.tostring(); if( '' == number_format ) { return number; } return number.split( /(?=(?:...)*$)/ ).join( number_format ); }, start_count = function(element, countto, increment, current, fakecountto, number_format) { //calculate the new number var newcount = current + increment, final = ''; //if the number is bigger than our final number set the number and finish if(newcount >= fakecountto) { final = format_number( countto, number_format, countto ); element.text( final ); //exit } else { final = format_number( newcount, number_format, countto ); element.text(final); window.requestanimationframe(function(){ start_count(element, countto, increment, newcount, fakecountto, number_format); }); } }; return this.each(function() { var number_container = $(this), elements = number_container.find('.__av-single-number'), counttimer = number_container.data('timer') || 3000; //prepare elements elements.each(function(i) { var element = $(this), text = element.text(); if(window.addeventlistener) element.text( text.replace(/./g, "0")); /*https://github.com/aviathemes/wp-themes/issues/812*/ }); //trigger number animation number_container.addclass('number_prepared').on('avia_start_animation', function() { if(number_container.is('.avia_animation_done')) return; number_container.addclass('avia_animation_done'); elements.each(function(i) { var element = $(this), countto = element.data('number'), fakecountto = countto, current = parseint(element.text(),10), zeroonly = /^0+$/.test(countto), increment = 0, number_format = element.data('number_format'); //fallback for decimals like 00 or 000 if(zeroonly && countto !== 0) fakecountto = countto.replace(/0/g, '9'); increment = math.round( fakecountto * 32 / counttimer); if(increment == 0 || increment % 10 == 0) increment += 1; settimeout(function(){ start_count(element, countto, increment, current, fakecountto, number_format);}, start_timer); }); }); if(options && options.instant_start == true) { number_container.trigger('avia_start_animation'); } }); }; })(jquery);