Skip to content

Commit f289ae4

Browse files
committed
chore: refactor count-up animation in users info cards with smoother easing and improved formatting
1 parent b0ba0f3 commit f289ae4

1 file changed

Lines changed: 21 additions & 14 deletions

File tree

resources/views/components/users/⚡info-cards.blade.php

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,27 +41,34 @@ public function render()
4141
<script>
4242
Alpine.data('usersInfoCardsPart', () => ({
4343
init() {
44-
document.querySelectorAll('.count-up').forEach((countUp) => {
45-
let from = 0;
46-
let to = Number(countUp.textContent);
44+
document.querySelectorAll('.count-up').forEach((el) => {
45+
const target = Number(el.textContent);
4746
48-
if (to > 999) {
49-
to = 999;
50-
}
51-
52-
if (from === to) {
47+
if (target <= 0) {
5348
return;
5449
}
5550
56-
let counter = setInterval(() => {
57-
countUp.textContent = String(from);
51+
const duration = 1500;
52+
const start = performance.now();
53+
54+
const format = (n) => n > 999
55+
? Math.floor(n / 1000) + 'k'
56+
: String(n);
57+
58+
const step = (now) => {
59+
const progress = Math.min((now - start) / duration, 1);
60+
// ease-out cubic: fast start, smooth deceleration
61+
const eased = 1 - Math.pow(1 - progress, 3);
62+
63+
el.textContent = format(Math.round(eased * target));
5864
59-
if (from === to) {
60-
clearInterval(counter);
65+
if (progress < 1) {
66+
requestAnimationFrame(step);
6167
}
68+
};
6269
63-
from++;
64-
}, 1000 * (1 / to));
70+
el.textContent = '0';
71+
requestAnimationFrame(step);
6572
});
6673
}
6774
}));

0 commit comments

Comments
 (0)