Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

Javascript number Count up

// How long you want the animation to take, in ms
const animationDuration = 2000;
// Calculate how long each ‘frame’ should last if we want to update the animation 60 times per second
const frameDuration = 1000 / 60;
// Use that to calculate how many frames we need to complete the animation
const totalFrames = Math.round( animationDuration / frameDuration );
// An ease-out function that slows the count as it progresses
const easeOutQuad = t => t * ( 2 - t );

// The animation function, which takes an Element
const animateCountUp = el => {
	let frame = 0;
	const countTo = parseInt( el.innerHTML, 10 );
	// Start the animation running 60 times per second
	const counter = setInterval( () => {
		frame++;
		// Calculate our progress as a value between 0 and 1
		// Pass that value to our easing function to get our
		// progress on a curve
		const progress = easeOutQuad( frame / totalFrames );
		// Use the progress value to calculate the current count
		const currentCount = Math.round( countTo * progress );

		// If the current count has changed, update the element
		if ( parseInt( el.innerHTML, 10 ) !== currentCount ) {
			el.innerHTML = currentCount;
		}

		// If we’ve reached our last frame, stop the animation
		if ( frame === totalFrames ) {
			clearInterval( counter );
		}
	}, frameDuration );
};

// Run the animation on all elements with a class of ‘countup’
const runAnimations = () => {
	const countupEls = document.querySelectorAll( '.countup' );
	countupEls.forEach( animateCountUp );
};
Comment

PREVIOUS NEXT
Code Example
Javascript :: nginx reverse proxy redirect 
Javascript :: load.json 
Javascript :: md5 checksum javascript 
Javascript :: redux toolkit 
Javascript :: angular style component tag 
Javascript :: vue js link image link in props doesnt work 
Javascript :: shift and unshift js 
Javascript :: react emoji picker 
Javascript :: javascript split 
Javascript :: mean stack 
Javascript :: how to find missing number in integer array of 1 to 100 in javascript 
Javascript :: loop through async javascript -4 
Javascript :: Promises ex. 
Javascript :: datatable on change event jquery 
Javascript :: Using flat() method 
Javascript :: remove an last item of array in javascript 
Javascript :: how to find a name of class from page in jquery 
Javascript :: C# Convert Json File to DataTable 
Javascript :: hoisting in javscript 
Javascript :: react native app crashing on start 
Javascript :: node .env file example 
Javascript :: react click outside class implementation 
Javascript :: play audio in react 
Javascript :: sequelize transaction util 
Javascript :: greater than x but less than y javascript 
Javascript :: node cache 
Javascript :: javascript add parameter to object 
Javascript :: how to clear a function in javascript 
Javascript :: export table data to excel using javascript or jquery 
Javascript :: delete message plugin discord 
ADD CONTENT
Topic
Content
Source link
Name
7+2 =