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

98
Visualizações
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 à 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