-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
42 lines (39 loc) · 1.46 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
document.addEventListener("DOMContentLoaded", function () {
const elements = Array.from(document.querySelectorAll("h3[data-count]"));
const observer = new IntersectionObserver(
function (entries) {
entries.forEach((entry) => {
if (entry.intersectionRatio >= 0.5) {
const index = elements.indexOf(entry.target);
const delay = index * 250;
setTimeout(() => startCounter(entry.target), delay);
observer.unobserve(entry.target);
}
});
},
{
threshold: 0.5
}
);
elements.forEach((element) => observer.observe(element));
function startCounter(element) {
const target = parseInt(element.getAttribute("data-count"));
const additionalData = element.getAttribute("additional-data") || "";
const totalFrames = parseInt(target);
let frame = 0;
function easeOut(t, b, c, d) {
return c * ((t = t / d - 1) * t * t + 1) + b;
}
function counter() {
if (frame >= totalFrames) {
element.textContent = target + additionalData;
return;
}
frame++;
const easedValue = easeOut(frame, 0, target, totalFrames);
element.textContent = Math.floor(easedValue) + additionalData;
requestAnimationFrame(counter);
}
counter();
}
});