Skip to content

Commit

Permalink
moved counter animation code to custom.js (#536)
Browse files Browse the repository at this point in the history
  • Loading branch information
therealharshit authored Nov 7, 2024
1 parent 5836557 commit 87a8eec
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 51 deletions.
52 changes: 51 additions & 1 deletion js/custom.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,54 @@ function toggleAnswer(answerId, element) {
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
});
});
50 changes: 0 additions & 50 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,53 +122,3 @@ $(document).ready(function(){
});
};
});

// 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
});
});

0 comments on commit 87a8eec

Please sign in to comment.