Skip to content

Commit 55f0131

Browse files
authored
Create Number_counter_animation.js
1 parent fd117d0 commit 55f0131

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Number_counter_animation.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
const counterAnim = (qSelector, start = 0, end, duration = 1000) => {
2+
const target = document.querySelector(qSelector);
3+
let startTimestamp = null;
4+
const step = (timestamp) => {
5+
if (!startTimestamp) startTimestamp = timestamp;
6+
const progress = Math.min((timestamp - startTimestamp) / duration, 1);
7+
target.innerText = Math.floor(progress * (end - start) + start);
8+
if (progress < 1) {
9+
window.requestAnimationFrame(step);
10+
}
11+
};
12+
window.requestAnimationFrame(step);
13+
};
14+
//#endregion - end of - number counter animation
15+
16+
document.addEventListener("DOMContentLoaded", () => {
17+
counterAnim("#count1", 10, 300, 1000);
18+
counterAnim("#count2", 5000, 250, 1500);
19+
counterAnim("#count3", -1000, -150, 2000);
20+
counterAnim("#count4", 500, -100, 2500);
21+
});

0 commit comments

Comments
 (0)