Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

151
Visualizações
Use getBoundingClientRect function to get viewport of element

I have question to answer. I want to run the animateValue function when the user scrolls to the viewport of an element, I tried using the getBoundingClientRect but it doesn't work. Anyone know how to fix it or how to do it, can you help me? Thank you guys


(function () {
                      var items = document.querySelector("#number");
                      function isElementInViewport(el) {
    var rect = el.getBoundingClientRect();
    return (
      rect.top >= 0 &&
      rect.left >= 0 &&
      rect.bottom <=
        (window.innerHeight || document.documentElement.clientHeight) &&
      rect.right <= (window.innerWidth || document.documentElement.clientWidth)
    );
  }
  if (isElementInViewport(items)) {
                        function animateValue(obj, start, end, duration) {
                            let startTimestamp = null;
                            const step = (timestamp) => {
                                if (!startTimestamp) startTimestamp = timestamp;
                                const progress = Math.min((timestamp - startTimestamp) / duration, 1);
                                obj.innerHTML = Math.floor(progress * (end - start) + start);
                                if (progress < 1) {
                                    window.requestAnimationFrame(step);
                                }
                            };
                            window.requestAnimationFrame(step);
                        }
  
                        const obj = document.getElementById("value");
                        const obj2 = document.getElementById("value2");
                        const obj3 = document.getElementById("value3");
                        const obj4 = document.getElementById("value4");
                        animateValue(obj, 0, 2000, 10000);
                        animateValue(obj2, 0, 200, 10000);
                        animateValue(obj3, 0, 98, 10000);
                        animateValue(obj4, 0, 5, 10000);
  }
  window.addEventListener("load", animateValue);
  window.addEventListener("resize", animateValue);
  window.addEventListener("scroll", animateValue);
                    })();

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda