From e6055d5754fd2b6900a771cad31f6afec230f601 Mon Sep 17 00:00:00 2001 From: Bashar Awaluddin Nafsah <140600759+basharawluddinn@users.noreply.github.com> Date: Thu, 13 Jun 2024 00:42:50 +0700 Subject: [PATCH] Update index.js ganti function --- index.js | 28 ++++++++++++++++++++++------ 1 file changed, 22 insertions(+), 6 deletions(-) diff --git a/index.js b/index.js index 2ae09ab..bc4433c 100644 --- a/index.js +++ b/index.js @@ -67,12 +67,28 @@ function animateNumber(element, endValue, duration) { } // Apply animation to elements with the class 'number' -const numberElements = document.querySelectorAll('.number'); -numberElements.forEach((element) => { - const endValue = parseInt(element.dataset.target); - const duration = 2000; - animateNumber(element, endValue, duration); -}); + function animateNumber(element, endValue, duration) { + let startValue = 0; + let startTime = null; + + function updateNumber(currentTime) { + if (startTime === null) startTime = currentTime; + const progress = Math.min((currentTime - startTime) / duration, 1); + const value = Math.floor(progress * endValue); + element.textContent = value.toString().padStart(element.dataset.target.length, '0'); + if (progress < 1) { + requestAnimationFrame(updateNumber); + } + } + requestAnimationFrame(updateNumber); + } + + const numberElements = document.querySelectorAll('.number'); + numberElements.forEach((element) => { + const endValue = parseInt(element.dataset.target, 10); + const duration = 2000; + animateNumber(element, endValue, duration); + }); // Toggle dropdown and link styles on click const dropdownProduct = document.getElementById('dropdown-product');