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

189
Visualizações
Translating html element using view width leave uneven spacing

Why does translateX using vw leave uneven spacing when there is a scrollbar?

When I translate the ul elements away from each other upon scrolling, the leftover space between the start of the viewport and the first ul is smaller than the space between the end of the viewport and the last ul. This only happen when there is a scrollbar in the page. Here are the snippets:

<nav class='navbar flex-container'>
  <ul class='nav-item__container flex-container'>
        ...
  </ul>
  <ul class='nav-item__container flex-container'>
        ...
  </ul>
</nav>
nav {
  display: flex;
  justify-content: center;
  align-items: center;
}

.nav-container.scrolled nav .nav-item__container:nth-child(1) {
  transform: translateX(calc(-50vw + 100%));
}

.nav-container.scrolled nav .nav-item__container:nth-child(2) {
  transform: translateX(calc(50vw - 100%));
}

I fixed this by getting the width of the scrollbr and adding it to the original translateX of the first ul element.

let scrollbarWidth = window.innerWidth - body.offsetWidth;

window.addEventListener('scroll', function() {
    // nav menu item animatate on scroll
    if (window.scrollY >= 10) {
        navContainer.classList.add('scrolled');
        navItemContainer.style.transform = `translateX(calc(-45vw + 100% + ${scrollbarWidth}px))`;
    } else {
        navContainer.classList.remove('scrolled');
        navItemContainer.removeAttribute('style');
    }
});
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