-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
65 lines (55 loc) · 1.77 KB
/
script.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
import { collapse, initMDB } from 'mdb-ui-kit';
initMDB({ collapse });
const swiper = new Swiper('.mySwiper', {
loop: true,
spaceBetween: 30,
centeredSlides: true,
autoplay: {
delay: 5000,
disableOnInteraction: false,
},
pagination: {
el: '.swiper-pagination',
clickable: true,
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
});
// counter
const counters = document.querySelectorAll('.counter');
const speed = 100; // Speed of the counting
// Function to animate the counting
const animateCounter = (counter) => {
const target = +counter.getAttribute('data-target'); // Get the target value
const increment = target / speed; // Calculate the increment step
let currentValue = 0;
const updateCounter = () => {
currentValue += increment;
if (currentValue < target) {
counter.textContent = Math.ceil(currentValue);
requestAnimationFrame(updateCounter);
} else {
counter.textContent = target;
}
};
updateCounter();
};
// Intersection Observer to start counting when in view
const observerOptions = {
threshold: 0.2 // When 20% of the counter is in view
};
const observerCallback = (entries, observer) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
animateCounter(entry.target); // Start counting
observer.unobserve(entry.target); // Stop observing once animation starts
}
});
};
const observer = new IntersectionObserver(observerCallback, observerOptions);
// Observe each counter
counters.forEach(counter => {
observer.observe(counter);
});