From c9c7a920121d97030f005e91fac352c6021d8ad1 Mon Sep 17 00:00:00 2001 From: X4Lo Date: Sun, 19 May 2024 17:41:54 +0100 Subject: [PATCH] fix: perfect scrollbar not scrolling to top after changing page --- frontend/src/app/app.component.ts | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/frontend/src/app/app.component.ts b/frontend/src/app/app.component.ts index 8f665bb..c0a7f2a 100644 --- a/frontend/src/app/app.component.ts +++ b/frontend/src/app/app.component.ts @@ -1,4 +1,7 @@ import { AfterViewInit, Component, ElementRef, ViewChild } from '@angular/core'; +import { NavigationEnd, Router } from '@angular/router'; +import { filter } from 'rxjs'; + import PerfectScrollbar from 'perfect-scrollbar'; @Component({ @@ -7,12 +10,25 @@ import PerfectScrollbar from 'perfect-scrollbar'; styleUrl: './app.component.css', }) export class AppComponent implements AfterViewInit { + title = 'ShowZ'; + @ViewChild('scrollContainer', { static: true }) scrollContainer!: ElementRef; private ps!: PerfectScrollbar; - title = 'ShowZ'; + constructor(private router: Router) {} ngAfterViewInit() { this.ps = new PerfectScrollbar(this.scrollContainer.nativeElement); + + this.router.events + .pipe(filter(event => event instanceof NavigationEnd)) + .subscribe(() => { + this.scrollToTop(); + }); + } + + scrollToTop() { + this.scrollContainer.nativeElement.scrollTop = 0; + this.ps.update(); } }