100-project-100-days-website/100_projects/32-increase-numbers-on-scrolling/main.js
2023-03-25 02:06:40 +03:00
Ask

21 lines
543 B
JavaScript

{a4d8e121aeee908500dab70d1c18e70820915755 true 543 main.js 0xc001fbeb60}

let nums = document.querySelectorAll(".nums .num");
let section = document.querySelector(".three");
let started = false; // Function Started ? No
window.onscroll = function () {
if (window.scrollY >= section.offsetTop) {
if (!started) {
nums.forEach((num) => startCount(num));
}
started = true;
}
};
function startCount(el) {
let goal = el.dataset.goal;
let count = setInterval(() => {
el.textContent++;
if (el.textContent == goal) {
clearInterval(count);
}
}, 2000 / goal);
}