My goal is to start a number counter from 0 to 100 as soon as the number enters the viewport.
With my current solution, I have two issues:
- The number jumps back to 0 once it finished counting to 100
- The counting doesn’t "start" when the element enters the viewport, but rather depends on the scroll state. It counts the further the user scrolls.
Here is my code:
@property --num {
syntax: "<integer>";
initial-value: 0;
inherits: false;
}
.number {
animation-name: counter;
animation-duration: 5s;
animation-timing-function: ease-in-out;
animation-delay: 0s;
animation-direction: normal;
animation-iteration-count: 1;
animation-fill-mode: none;
animation-timeline: view();
counter-reset: num var(--num);
font: 800 40px system-ui;
padding: 2rem;
}
.number::after {
content: counter(num);
}
@keyframes counter {
from {
--num: 0;
}
to {
--num: 100;
}
}
<span class="number"></span>
2
Answers
You have a few things that need to be updated for this to work.
num
variable in a @property blockanimation-timeline
animation
(forwards
will stop the fill-mode on its last frame).Something like the following:
You need to define the animation range. In your case,
animation-range: entry 100% exit 0%;
. The considerforwards
orboth
to make sure the final value remain after the animation ends