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

100
Vistas
Dynamic scroll margin top value with a sticky header?

I have a sticky header which changes the size (the logo becomes smaller) when the user scroll down the page, for example:

  • header height with no scrolling: 200px
  • header height with scrolling: 100px

Below the header, there is a list of links which will jump to the respective position when a user click on them. I have added scroll-margin-top for smooth scrolling and to prevent the elements to be "hidden" behind the sticky header. The scroll-margin-top value is perfect when the page is not scrolled, meaning the header still has the original height. It doesn't look "perfect" anymore, as soon as the page is scrolled. The user will see a bit of text from the previous link. I assume this has something to do with the smaller header size.

Does anyone has an idea how I can have a "dynamic" scroll-margin-top value based on the header size? Or is there any other way I can achieve a similar behaviour?

    // Sticky header on scroll
    //    async firstUpdated() {
    //     await this.updateComplete;

    //     window.addEventListener('scroll', this.checkSticky, { passive: true });
    //     window.addEventListener('resize', this.checkSticky);
    // }

    // disconnectedCallback() {
    //     window.removeEventListener('scroll', this.checkSticky);
    //     window.removeEventListener('resize', this.checkSticky);
    // }

    // checkSticky = async ({ type: eventType }) => {
    //     if (
    //         window.pageYOffset > 0
    //         && (!this.isSticky || eventType === 'resize')
    //     ) {
    //         this.isSticky = true;

    //         window.dispatchEvent(Header.eventIsSticky);
    //     } else if (
    //         window.pageYOffset <= 0
    //         && (this.isSticky || eventType === 'resize')
    //     ) {
    //         this.isSticky = false;

    //         window.dispatchEvent(Header.eventNotSticky);
    //     }
    // }
/* Scroll margin top
body * {
  scroll-margin-top: 200px
 }
 */

Thank you very much for your help!

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