From c775ed47c1e403faf31428884630b7526648f85c Mon Sep 17 00:00:00 2001 From: Ayushi Choudhary <73214455+ayushichoudhary-19@users.noreply.github.com> Date: Tue, 12 Sep 2023 16:03:10 +0530 Subject: [PATCH] Update Hero.jsx --- src/sections/Hero.jsx | 47 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 45 insertions(+), 2 deletions(-) diff --git a/src/sections/Hero.jsx b/src/sections/Hero.jsx index ea0b6ea..30ab241 100644 --- a/src/sections/Hero.jsx +++ b/src/sections/Hero.jsx @@ -5,10 +5,53 @@ import { arrowRight } from "../assets/icons" import { shoes, statistics } from "../constants" import { bigShoe1 } from "../assets/images" import ShoeCard from "../components/ShoeCard" -import { useState } from "react" +import { useState, useEffect } from "react" + +const formatNumber = (number) => { + if (number < 1000) { + return number; + } else { + return Math.floor(number / 1000) + "k"; + } +}; const Hero = () => { const [bigShoeImg, setBigShoeImg] = useState(bigShoe1) + const [animatedStatistics, setAnimatedStatistics] = useState({ + brands: 0, + shops: 0, + customers: 0, + }); + useEffect(() => { + const animationDuration = 2500; + const targetStatistics = { + brands: 1000, + shops: 500, + customers: 250000, + }; + + const stepDuration = animationDuration / 100; + + const updateStatistics = (currentStep) => { + if (currentStep >= 100) { + setAnimatedStatistics(targetStatistics); + } else { + setTimeout(() => { + const percentage = (currentStep + 1) / 100; + setAnimatedStatistics({ + brands: Math.floor(percentage * targetStatistics.brands), + shops: Math.floor(percentage * targetStatistics.shops), + customers: Math.floor(percentage * targetStatistics.customers), + }); + updateStatistics(currentStep + 1); + }, stepDuration); + } + }; + + updateStatistics(0); + + }, []); + return (
@@ -23,7 +66,7 @@ const Hero = () => {
{statistics.map(stat => (
-

{stat.value}

+

{formatNumber(animatedStatistics[stat.label.toLowerCase()])}+

{stat.label}

))}