Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const counters = document.querySelectorAll('.counter');
- const speed = 400;
- counters.forEach(counter => {
- const updateCount = () => {
- const target = +counter.getAttribute('data-counter');
- const count = +counter.innerText.replaceAll(',', '');
- const inc = target / speed;
- if (count < target) {
- counter.innerText = Math.ceil(count + inc).toLocaleString('en-US');
- setTimeout(updateCount, 1);
- } else {
- counter.innerText = target.toLocaleString('en-US');
- }
- };
- const observeElement = function(elements) {
- const element = elements[0];
- if (element.isIntersecting) {
- updateCount();
- observer.unobserve(counter);
- }
- };
- const observer = new IntersectionObserver(observeElement);
- observer.observe(counter);
- });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement