Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

190
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda