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

300
Visualizações
JavaScript: Scrolling to an item relative to user viewport (screen) WITH OFFSET

I'm trying to create a button that scrolls to a particular element that is further down on the page. I have managed to create a function that allows me to scroll to the top offset of the item like this:

const scrollToIntro = () => {
  const viewPortOffSet = window.document
    .getElementById("introduction")
    .getBoundingClientRect();
  window.scrollTo({
    top: viewPortOffSet.top + window.scrollY,
    behavior: "smooth",
  });
};

However, I find that scrolling to the top to be a little too much in the sense that have scrolled too far down and would like to subtract some value from top relative to the viewport. One way I've found is to multiply the number by a factor like this:

const scrollToIntro = () => {
  const viewPortOffSet = window.document
    .getElementById("introduction")
    .getBoundingClientRect();
  window.scrollTo({
    top: viewPortOffSet.top * 0.95 + window.scrollY,
    behavior: "smooth",
  });
};

But I am wondering if there are alternatives that can maybe involve subtraction of values using "vh" etc.

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