jacodelucia
5/11/2016 - 5:14 PM

JS - Animation chiffres (increment)

JS - Animation chiffres (increment)

/************************************************************************************************/
/* ANIMATION CHIFFRES */
/************************************************************************************************/
	
	function checkChiffres(elem, checkMore){
		checkMore = checkMore || false;
		if(checkMore == true){
			if( elem.hasClass('module-chiffres') && ! elem.hasClass('doneMore') ){
				var elems = elems = elem.find('.increment.more');
				elem.addClass('doneMore');
				elems.each(function(){
					incrementChiffre($(this));
				});
			}
		} else {
			if( elem.hasClass('module-chiffres') && ! elem.hasClass('done') ){
				var elems = elem.find('.increment').not('.more');
				elem.addClass('done');
				elems.each(function(){
					incrementChiffre($(this));
				});
			}
		}
	}
	
	function incrementChiffre(elem){
		var targetNumber = elem.data('int');
		$({intValue: 0}).animate({intValue: targetNumber}, {
			duration: 2000,
			easing:'swing', 
			step: function() { 
				elem.html(spaceSeparateNumber(Math.round(this.intValue)));
			}
		});
	}
	
	function spaceSeparateNumber(val){
		while (/(\d+)(\d{3})/.test(val.toString())){
			val = val.toString().replace(/(\d)(?=(\d\d\d)+(?!\d))/g, "$1 ");
		}
		return val;
	}