Count Animation
The count animation component is designed to create a captivating user experience by animating numerical values as they increase or decrease. This effect draws the user's attention to important data, making the information more dynamic and engaging. Whether it's displaying statistics, financial figures, or progress counters, the smooth animation of numbers adds a sense of liveliness and interactivity to your interface. This example was created using Tailwind CSS and Framer Motion.
Examples
0
Install the following dependencies:
npm i framer-motion clsx tailwind-merge
Add util file
import { ClassValue, clsx } from 'clsx';
import { twMerge } from 'tailwind-merge';
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
}
Copy and paste the following code into your project.
"use client";
import { cn } from "@/lib/utils";
import { motion, useMotionValue, useTransform, animate } from "framer-motion";
import { useEffect } from "react";
export default function CountAnimation({
number,
className,
}: {
number: number;
className: string;
}) {
const count = useMotionValue(0);
const rounded = useTransform(count, Math.round);
useEffect(() => {
const animation = animate(count, number, { duration: 2 });
return animation.stop;
}, []);
return <motion.h1 className={cn(className)}>{rounded}</motion.h1>;
}
Usage
import CountAnimation from "@/components/ui/count-animation";
export default function CountAnimationExamle() {
return (
<>
<CountAnimation number={60} className="text-4xl" />
</>
);
}