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

403
Vistas
Angular scrollToAnchor not working with flex

I have app component which has following html

<app-header></app-header>
<div class="main-container">
    <router-outlet></router-outlet>
</div>
<app-footer></app-footer>

with css

:host {
  display: flex;
  flex-direction: column;
  height: 100%;
}

.main-container {
  display: flex;
  flex-grow: 1;
  overflow: auto;
}

and component in router-outlet looks like this:

<div id="home">
</div>

<div id="about">
</div>

<div id="contact">
</div> 

I'm trying to add scrollToAnchor, but it's not working. It works if I remove overflow: auto from main container, but then whole page is scrollable, and header and footer are not static. Is there way to enable scrollToAnchor with flex layout and only scrolling middle part of app? I'd prefer flex instead of position: fixed.

main.component.ts:

constructor(
        router: Router,
        viewportScroller: ViewportScroller
    ) {

        router.events.pipe(
            filter(e => e instanceof Scroll)
        ).subscribe(e => {
            e = e as Scroll
            if (e.anchor) {
                // anchor navigation
                viewportScroller.scrollToAnchor(e.anchor);
            }
        });
    }

html that triggers scrollToAnchor

<a mat-button routerLink="/" fragment="home"></a>
<a mat-button routerLink="/" fragment="about"></a>
<a mat-button routerLink="/" fragment="contact"></a>
over 4 years ago · Santiago Trujillo
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