CSS Animation to Increment Counter

Viewed 2994

I've been playing around with CSS counters lately and I wondered if it was possible to put them in a CSS animation loop and increment them. My code is fairly simple:

div {
    animation-name: anim;
    animation-duration: 0.5s;
    animation-iteration-count: 10;
    animation-fill-mode: forwards;
}

div:before {
    counter-increment: variable;
    content: counter(variable);
}

@keyframes anim {
    100% { counter-increment: variable; }
}
<div></div>

You can see the number goes up, but then it snaps back to 1. I assumed the animation-fill-mode: forwards would prevent that from happening, but I guess not.

Am I doing something wrong, or is this not possible with CSS counters?

1 Answers

It can be easily achieved using a simple script code, try this

<div><span class="count">200</span></div>
<div><span class="count">177</span></div>

$('.count').each(function () {
    $(this).prop('Counter',0).animate({
       Counter: $(this).text()
    }, {
       duration: 4000,
       easing: 'swing',
       step: function (now) {
          $(this).text(Math.ceil(now));
       }
   });
});

Refer This

Related