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

202
Visualizações
Correct way to get Scroll Position

In my application, I want to determine the current scroll position, so I have been using this to do so (source: https://www.codegrepper.com/code-examples/javascript/get+current+scroll+position+javascript)

window.pageYOffset || document.documentElement.scrollTop 

Problem: When I console.log this, it only returns 0 when the page is not positioned and the top.

Question: Is there an alternative way to get the current scroll position?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

recently i had the same problem. So in order to get the position as a percentage I end up do the following

window.addEventListener("scroll", function() {

    const maxHeight = document.body.scrollHeight - window.innerHeight;
    console.log((window.pageYOffset * 100) / maxHeight);
});

We subtract window.innerHeight from document.body.scrollHeight besause window.pageYOffset represents the top of the viewport. So in order for window.pageYOffset to match document.body.scrollHeight we do the above subtraction.

PS.: The above returns a float number. you can use parseInt(...) to convert it to integer if you'd like.

about 4 years ago · Juan Pablo Isaza Relatório

0

Try window.scrollY.

The read-only scrollY property of the Window interface returns the number of pixels that the document is currently scrolled vertically.

https://developer.mozilla.org/en-US/docs/Web/API/Window/scrollY

about 4 years ago · Juan Pablo Isaza Relatório
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