forked from sugarlabs/www
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcustom.js
65 lines (53 loc) · 1.93 KB
/
custom.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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//show answer and change indicator for /FAQ page
function toggleAnswer(answerId, element) {
const answer = document.getElementById(answerId);
const indicator = element.querySelector('.indicator');
if (answer) {
answer.classList.toggle('d-none');
if (answer.classList.contains('d-none')) {
indicator.innerHTML = '+';
} else {
indicator.innerHTML = '-';
}
}
}
// Counter Animation of the home page
document.addEventListener('DOMContentLoaded', () => {
const counters = document.querySelectorAll('.count');
// Function to animate the counter
function animateCounter(counter) {
const target = +counter.getAttribute('data-target');
const speed = 200;
const updateCount = () => {
const current = +counter.innerText;
const increment = target / speed;
if (current < target) {
counter.innerText = Math.ceil(current + increment);
setTimeout(updateCount, 10); // Repeat every 10ms
} else {
if (target === 170) {
counter.innerText = `${target}`;
} else {
counter.innerText = `${target}+`;
}
}
};
updateCount();
}
// Use IntersectionObserver to detect when the element comes into view
const observerOptions = {
root: null,
threshold: 0.3
};
const observer = new IntersectionObserver((entries, observer) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
animateCounter(entry.target); // Start animation
observer.unobserve(entry.target); // Stop observing after animation starts
}
});
}, observerOptions);
counters.forEach(counter => {
observer.observe(counter); // Observe each counter
});
});