-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
30 lines (29 loc) · 970 Bytes
/
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
document.addEventListener("DOMContentLoaded", () => {
function counter(id, start, end, duration, suffix = '') {
let obj = document.getElementById(id);
let current = start;
let range = end - start;
let increment = end > start ? 1 : -1;
let step = Math.abs(Math.floor(duration / range));
const observer = new IntersectionObserver((entries) => {
console.log(entries);
entries.forEach((entry) => {
if (entry.isIntersecting) {
const timer = setInterval(() => {
console.log("Current value:", current);
current += increment;
obj.textContent = current + suffix;
if (current === end) {
clearInterval(timer);
}
}, step);
observer.unobserve(obj);
}
});
});
observer.observe(obj);
}
counter("count1", 0, 31, 3000, '%');
counter("count2", 0, 22, 2500, '%');
counter("count3", 0, 500, 3000, '+');
});