Animated Counter With JavaScript (HTML, CSS)

Поделиться
HTML-код
  • Опубликовано: 5 фев 2025
  • In this mini-project, I will show you how to create an animated counter that counts up to any number using JavaScript with HTML & CSS.
    Code:
    codepen.io/bra...
    💖 Become a Patron: Show support & get perks!
    / traversymedia
    Website & Udemy Course Links:
    www.traversyme...
    Follow Traversy Media:
    / traversymedia
    / traversymedia
    / traversymedia
  • НаукаНаука

Комментарии • 264

  • @accimeesterlin
    @accimeesterlin 5 лет назад +37

    Keep it up man! You’re contributing a hell lot in the tech community

  • @mohamaddaylami174
    @mohamaddaylami174 3 года назад +1

    Everytime I look for something after searching YT I come back to your channel. Sir you are amazing... God bless you🙏💐

  • @animationbysophia2820
    @animationbysophia2820 5 лет назад +24

    I love the way you teach sir.
    Just wanna say THANK YOU.
    All you efforts means a lot.
    THANK YOU SO MUCH.
    Love and respect from Pakistan.

  • @ebentee
    @ebentee 5 лет назад +76

    Whoever read this you'll be successful one day, let's do this together for the future

  • @techlead...
    @techlead... 5 лет назад +111

    *" 1.01M Subscribers "*
    looks so good on your channel.

  • @TheNerdyDev
    @TheNerdyDev 5 лет назад +34

    Awesome content as always. I recommend your tutorials always to my juniors.

    • @TraversyMedia
      @TraversyMedia  5 лет назад +4

      Thanks :)

    • @ob5804
      @ob5804 2 года назад

      @@TraversyMedia And I cannot explain to myself what an increment is(((???

  • @victorschweitz3506
    @victorschweitz3506 5 лет назад

    I was about to follow a tutorial on how to create a website with some different stats, and I thought it would look cool to have a counting animation. And guess what popped up in my feed of recommendations, and by whom it was done. Yes, you've probably already guessed it. Brad Traversy is the man!

  • @accimeesterlin
    @accimeesterlin 5 лет назад +1

    That 1.01M is so legit on this channel. You really deserve it man!!

  • @yendrembamrabinchandra476
    @yendrembamrabinchandra476 5 лет назад +2

    Not only programming, I also learn English from Brad and that's why I never fail to watch every single he uploaded😁

  • @gguddum
    @gguddum 3 года назад

    I've googled for 2days, but couldn't find any codes that not used the library. but your codes solved my problem, thank you!! 👍👍👍

  • @devabdultech
    @devabdultech 5 лет назад

    I can't just stop watching your videos. Thank for your time and making sure we learn from your tutorials. I wish to meet you one day. From Nigeria I'm 15. And Iearn pretty good from you

  • @muhammadshakir3688
    @muhammadshakir3688 5 лет назад

    You are my idol as a web developer. You have helped me to get my first job and maybe second one soon!!!! Thank you

  • @vikasni95
    @vikasni95 5 лет назад +1

    Brad ur just god gifted teacher for the students on earth... Lots of love and support from india 🇮🇳...

  • @hasimabck6694
    @hasimabck6694 5 лет назад

    You know what, it makes me smile when to see a new video alert from Traversy Media. And especially if the content is something that I was looking for a long time. Thank you man. Best lucks in your life.

  • @mazdaknazemi8207
    @mazdaknazemi8207 5 лет назад

    need more of these works brad , there are many of these works , but with low clarity , thank you.

  • @abdullahalsiam105
    @abdullahalsiam105 5 лет назад

    i love you brad. you are awesome. you teach me HTML CSS. now i am learning javascript. the way you explain things are really easy but helpful. god bless you Brad.

  • @Fer0710
    @Fer0710 5 лет назад

    Thanks so much brad, this is great! These type of tutorials are the actual foundation. Everything is so saturated with frameworks libraries that some JR devs forget that this is the bread and butter here.... Thank you for all your tutorials and for everything you teach man!

  • @danilove9106
    @danilove9106 5 лет назад

    We need thousands of project like this from you !

  • @mattbridges385
    @mattbridges385 5 лет назад +1

    Love these little shorter one-shot design element ideas to add to our toolbox! keep up the good work!

  • @Omerko
    @Omerko 5 лет назад +3

    Small JS code for great feature. Thanks as always and Congratz on 1M subs! You deserved it!

  • @icytubegram8979
    @icytubegram8979 5 лет назад

    YOU ARE THE BEST ALWAYS DOING WHAT'S ON MY MIND, JUST THE OTHER DAY I WAS LOOKING FOR THIS TUTORIAL AND I LOVE TO TRY TRAVERSY MEDIA FIRST ALTHOUGH I DID NOT SEE IT THEN I'M REALLY SURPRISED THAT FEW DAYS AFTER MY SEARCH , NOW BOOM!!!! "THE AWESOME BRAD TRAVERSY MADE IT A REALITY ". MAY GOD KEEP YOU ALIVE AND EVERY OTHER TECH TUTORS DOING IT YOUR WAY "YOU ARE A STAR IN THE TECH WORLD"

  • @gbagba81
    @gbagba81 3 года назад

    thanks man, lot of heart and really usefull. people reading you may figure this easily, but if you are sleepy as I am right now you will want to put counter.innerText instead of coun.innerText. Other way it'll write mistaken decimals. Cheers!

  • @claudiobras22
    @claudiobras22 4 года назад

    Sir, allow me to say this. You are good and amazing at what you do. I admire your work. You inspire me a lot.

  • @mluevanos
    @mluevanos 4 года назад

    Love the counter script. Very elegant and easy to read.

  • @smsibasish
    @smsibasish 5 лет назад +7

    Adding a fade in effect on scroll would have been the cherry on top 😜

  • @chrisdevcode
    @chrisdevcode 5 лет назад

    This video came in handy Brad... You really helping me a lot over here... With love from Kenya

  • @rangabharath4253
    @rangabharath4253 5 лет назад

    Awesome as always. Congratulations on 1M Subscribers. More to come. All the best. Please make awesome tutorials as always.

  • @accimeesterlin
    @accimeesterlin 5 лет назад

    Good stuff. HTML, CSS, and JavaScript is always a good idea for beginners

  • @sylwesterjot921
    @sylwesterjot921 5 лет назад

    This guy is my mentor.

  • @lilJ5754
    @lilJ5754 4 года назад

    That was super sick at 13:12

  • @gkossecurecommunitynetwork7112
    @gkossecurecommunitynetwork7112 4 года назад

    Brilliant video i will be changing a few bits in the code but this a huge time saver for a newbie to JS.

  • @ricardofynn
    @ricardofynn 5 лет назад

    > 1M congrats. Deserve, great channel 👊

  • @cameronthompson1275
    @cameronthompson1275 5 лет назад

    Once again, fantastic work! You are a web dev ninja!

  • @philippmerk5980
    @philippmerk5980 4 года назад

    best js tutorials out there. THANKS!!!

  • @parthkumarchaudhary
    @parthkumarchaudhary 5 лет назад +5

    Awesome.
    1.01 million subs finally Awesome.
    Waiting for your giveaway 😋

  • @venussmodzhd1886
    @venussmodzhd1886 4 года назад

    Great Video man. I learn much better with you man. your so easy to understand :)

  • @ebrimao.touray8201
    @ebrimao.touray8201 5 лет назад

    Your tutorials are the best 😊

  • @matiasbacelar8662
    @matiasbacelar8662 5 лет назад

    Great as always Brad , cheers from Argentina

  • @TheSilmarallion
    @TheSilmarallion 4 года назад

    Awesome was looking for something like this for a while. Great tut and great tutor...

  • @gustavocarneirinho5581
    @gustavocarneirinho5581 5 лет назад +2

    Wow, we always have something new to learn, I never saw this way of converting string to number using only +, I always use Number, parseInt or parseFloat, very good!

    • @christopherc7069
      @christopherc7069 4 года назад +1

      I thought this was cool too. I guess it works because the engine is coercing it to a number type since he's trying to perform addition on it?

    • @h.kubilay6160
      @h.kubilay6160 4 года назад +1

      @@christopherc7069 it is called unary plus.

  • @phichau90
    @phichau90 5 лет назад +2

    Just started learning php becoz of u dude. Loving it so far . abit of a novice but at least an 1hr or 2 each day reding an practicing.

    • @TraversyMedia
      @TraversyMedia  5 лет назад +2

      Check my PHP Front To Back series. It is a few years old but it is all fundamentals so all still relevant.

  • @Racindify
    @Racindify 4 года назад +4

    For anyone wanting to make this as an onscroll event here is a version I made. (I also added in comma's in the final display):
    window.onscroll = function() {
    let windowPos = window.scrollY;
    let counterPos = document.querySelector('.counter').offsetTop;
    let scrollTrigger = counterPos - window.innerHeight + 200;
    if (windowPos >= scrollTrigger) {
    const counters = document.querySelectorAll('.counter__number');
    const speed = 551; // The lower the slower

    counters.forEach(counter => {
    const updateCount = () => {
    const target = +counter.getAttribute('data-target');
    const count = +counter.innerText;
    const inc = target / speed;

    // Check if target is reached
    if (count < target) {
    counter.innerText = Math.ceil(count + inc);
    setTimeout(updateCount, 100);
    } else {
    counter.innerText = target.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
    }
    };

    updateCount();
    });
    }
    }

    • @Momoplazz
      @Momoplazz 3 года назад

      not working for me

    • @Momoplazz
      @Momoplazz 3 года назад

      should we add counter__number to that counter div?

  • @unchart_d
    @unchart_d 5 лет назад

    Wat can we do without brad?😂😂

  • @bank_coder
    @bank_coder 5 лет назад

    Just what my client needed ... thanks

  • @m3awna
    @m3awna 5 лет назад

    thanks a lot from Algeria

  • @webbase8836
    @webbase8836 5 лет назад

    Wow I was just thinking about how to do this. I've followed ur bootstrap course on udemy. Wow ur rili doing an excellent job and ur inspiring odas lyk me. Thank you

  • @boriskleshch8416
    @boriskleshch8416 4 года назад +1

    Thanx Brad, very interesting and simple!

  • @your_helper001
    @your_helper001 6 месяцев назад

    Whenever I want to find some material for learning , i always wanted to be your video.
    your way of telling things helps me a lot . and I am starting to be a .NET developer.
    firstly , front -end then move forward.
    can you atleast help me about which things and frameworks should i work on and learn and practice.
    if you have time please . THANKS FOR YOUR DEDICATION AND HARD WORK IN ALL THOSE YEARS.

  • @ittec6756
    @ittec6756 4 года назад

    Very good styling

  • @JamieMcGibbon
    @JamieMcGibbon 5 лет назад

    Great video! Thanks for all of the effort you put into making this, and all other videos! It’s appreciated!

  • @WW-nk7wg
    @WW-nk7wg 5 лет назад +3

    I have a point to make and a question.
    Point to make: There is an issue of overflowing the target number when the data-target values are not divisible by the speed value. For example if the target-value is let's say 60010, and speed is 200 it would count up to 60200. So we would need to check if we are over the target value in the if statement or maybe apply additionally min function like: Math.min(Math.ceil(count+inc), target)
    Question: Let's say we have this counter div somewhere down below our webpage. We want to start the count when we scroll down to the div and it becomes visible, rather than when the page loads. What is your suggested way of doing this?
    Great job as always! Thanks for all this free info!

    • @ashiinsane90
      @ashiinsane90 4 года назад +1

      To make it animate when it reaches the desired location u can use getBoundingClientRect().top look it up.
      u can use it like this if(desiredLocation.getBoundingClientRect().top < window.innerHeight{
      then do this;
      }

    • @astikdahal1363
      @astikdahal1363 4 года назад

      const counterSection = document.querySelector(".counters");
      function incCounter(){
      const counters = document.querySelectorAll('.counter');
      const speed = 200; // The lower the slower
      counters.forEach(counter => {
      const updateCount = () => {
      const target = +counter.getAttribute('data-target');
      const count = +counter.innerText;
      // Lower inc to slow and higher to slow
      const inc = target / speed;
      // Check if target is reached
      if (count < target) {
      // Add inc to count and output in counter
      counter.innerText = count + inc;
      // Call function every ms
      setTimeout(updateCount, 1);
      } else {
      counter.innerText = target;
      }
      };
      updateCount();
      });
      }
      const counterSectionOptions = {
      rootMargin: "-200px 0px 0px 0px"
      };
      const counterSectionObserver = new IntersectionObserver(function(
      entries,
      counterSectionObserver
      ) {
      entries.forEach(entry => {
      if (!entry.isIntersecting) {
      return;
      } else {
      incCounter();
      }
      });
      },
      counterSectionOptions);
      counterSectionObserver.observe(counterSection);
      Done using Intersection Observer :)

  • @338-p5e
    @338-p5e 2 года назад

    Thank you very much. Very good tutorial.

  • @georgettebeulah4427
    @georgettebeulah4427 5 лет назад

    This make so much sense

  • @davidconnelly
    @davidconnelly 5 лет назад

    The way things are going, you'll need a considerably smaller font size for your webpage, Brad. Congrats on the mil, dude.

  • @davearrebola
    @davearrebola 4 года назад

    You legend, thank you for everything you do.

  • @FurtherReading
    @FurtherReading 5 лет назад

    Veeery cool, Brad! Thank you, cheers from Thailand...going to the beach, now :) !

  • @vicks711
    @vicks711 4 года назад

    You are awesome. Thanks for sharing your knowledge.

  • @cryptopinnacle7658
    @cryptopinnacle7658 5 лет назад

    Wow I learnt a lot. Thank you Brad

  • @maskman4821
    @maskman4821 5 лет назад +2

    thanks for this creative tutorial, actually we don't need to write media query to achieve two-column, with grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)) the column container responds to different device width (including 700px) automatically, so we don't need media query anymore unless specific needs !!!

  • @alikazemi2028
    @alikazemi2028 5 лет назад

    i`m very grateful for this tutorial

  • @pierre8519
    @pierre8519 5 лет назад

    Really clear, really good !

  • @NikhilAdiga
    @NikhilAdiga 5 лет назад

    Thanks for the awesome content you put out Brad!

  • @RobizShow
    @RobizShow 4 года назад

    Nice Tutorials. Totally loved it!

  • @HeryYustana
    @HeryYustana 5 лет назад

    So excited to watch
    .. keep it up bruh...

  • @MrBaddoger
    @MrBaddoger 3 года назад

    VERY Helpful... Thanks

  • @ahmadgamal-eldin7732
    @ahmadgamal-eldin7732 4 года назад

    helpful as usual :)
    Many thnx, bro !

  • @abhaykumar9806
    @abhaykumar9806 4 года назад

    Love from India

  • @qureshihasnain
    @qureshihasnain 5 лет назад +4

    Please make video on your tutorials behind the scenes please brad I'm your biggest fan in programming world please accept my request brad
    Love from Pakistan

  • @universenerdd
    @universenerdd 4 года назад

    "1.07 million subs"
    looks so good on your channel

  • @BrettCahill
    @BrettCahill 4 года назад

    Amazing as per 👍

  • @fritzgamingandasmr25
    @fritzgamingandasmr25 Год назад +1

    How to put comma in thousand number?

  • @nocturno7887
    @nocturno7887 4 года назад

    Thank you Brad

  • @ebentee
    @ebentee 5 лет назад

    Also thanks for sharing, I really like you for your good work, blessed you

  • @vanessarufo8976
    @vanessarufo8976 4 года назад

    Great video!

  • @stefanstroie1154
    @stefanstroie1154 5 лет назад

    Exactly what i need bro, thank u a lot

  • @MrTHOMASisBack
    @MrTHOMASisBack 3 года назад

    Why the 4 numbers grow in parallel ? In the loop it should the first number reach the target then the second etc... Does this have to do with the fact that with setInterval we made updateCount a recursive function ?

  • @ashiinsane90
    @ashiinsane90 4 года назад +1

    Great tutorial. Finally a tutorial with pure javascript. I have a question though, when the numbers are much smaller lets say 90. It tends to convert it into float number when its animating. I think its because the division. Any idea how to fix this?

  • @evgeniy497
    @evgeniy497 5 лет назад

    Inspired your prev video about Microsoft clone, I've created Apple homepage clone. Thanks for useful and inspiration videos!

  • @ilqarbedirov3204
    @ilqarbedirov3204 4 года назад

    thank you it is very good for me

  • @diasdamen
    @diasdamen 4 года назад

    Thank you man. How to set a timer to each counting items for serial counting?

  • @adeltahenti1330
    @adeltahenti1330 5 лет назад

    Great job, thank you brad

  • @sahaneakanayaka3394
    @sahaneakanayaka3394 5 лет назад

    Thanks Brad ..... Keep it up.

  • @kushaldavda5892
    @kushaldavda5892 4 года назад +1

    The numbers show up in fractions if the target is less than the speed. Also math.floor doesn't work on the same. Any solutions for that?

    • @kushaldavda5892
      @kushaldavda5892 4 года назад +1

      I found the solution just use counter.innerText = Math.ceil(count + inc);

  • @videocasetteTV
    @videocasetteTV 5 лет назад

    Awesome as always , I want to learn javascript from scratch, which playlist or course should i follow, thanks in advance

  • @AyushSethGuitarCovers
    @AyushSethGuitarCovers 3 года назад

    How can you make the counting slow down as it reaches the final value?

  • @MrKiemsi
    @MrKiemsi 3 года назад

    Do we need to use clearTimeOut for this?

  • @nsquarens2s
    @nsquarens2s 4 года назад

    Awesome tutorial! Just checking if its possible to make the target value dynamic so it increment by 200 everyday? Thanks

  • @unknownman1
    @unknownman1 5 лет назад +9

    *hey Brad, what are your thoughts on Headless wordpress with React for freelancing??*

    • @TraversyMedia
      @TraversyMedia  5 лет назад +27

      I think it's a good idea. Wordpress is great for clients to be able to update their own content and using React allows you to have a fast and customized UI. Some other alternatives to WP are Contentful, Strapi, Prismic. I plan on making a video on at least one of them

    • @ApurvKhare
      @ApurvKhare 5 лет назад

      Strapi with React would be nice.

    • @zenahrb8316
      @zenahrb8316 5 лет назад

      @@TraversyMedia Strapi + VueJS would be lovely too.

  • @klugkluk
    @klugkluk 5 лет назад +1

    You have a slight error in your Codepen version. The comment next to "speed" says "the lower the slower", but it's the other way around.

  • @buka.a
    @buka.a 5 лет назад +1

    Hey Brad, the time you added a "+" to a string character and it converted to numbers, does that happen in all cases or just specifically cause it was a data-target attribute?

    • @Golodafunk
      @Golodafunk 4 года назад

      That happens always, it is a unary operator equivalent to do Number(value)

  • @khotambakhromov
    @khotambakhromov 5 лет назад +1

    Why other counters are not waiting for the previous one to count until the target and then start counting? Why they are all counting at the same time?

  • @drieq
    @drieq 9 месяцев назад

    Has something changed in JS? This is not working for me. From the first console.log(target) it's not showing anything. I put a solo console.log("Hello world") in the file and that is showing up. Any help would be appreciated!

  • @khancross
    @khancross 2 года назад

    Thanks for this nice tutorial. How is it possible to show a value in Hours:Minutes:Seconds likte total running time?

  • @mohammadsadman8567
    @mohammadsadman8567 5 лет назад

    Going to add this tomorrowwwww

  • @jeromesnail
    @jeromesnail 5 лет назад

    Great video.
    I would've used the dataset property to access the HTML5 data attribute.
    And maybe iteration over recursion in this case.

  • @accimeesterlin
    @accimeesterlin 5 лет назад

    Great content

  • @tech_revolution5175
    @tech_revolution5175 5 лет назад

    You are a great soul

  • @TurboNrrdFPV
    @TurboNrrdFPV 4 года назад

    Hey you rock sir!

  • @mattgreenia4279
    @mattgreenia4279 3 года назад

    Love the solution! I am experiencing an issue that I hope you can shed some light on... I deployed this solution in a script web part on a Modern style page in SharePoint. The script fires perfectly when you first go to the page. However, when you navigate internally on the site and go back to the page with the script it fails to fire. I've tried wrapping the code in a function and calling it on page load, to no avail. Thoughts?

  • @typicalyorkshirebloke
    @typicalyorkshirebloke 2 года назад

    Is it possible to have the numbers going up at different speeds inside of one set of code instead of repeating the exact same code over multiple times with different speeds?