1- import { Component , OnInit , OnDestroy , inject , Inject } from "@angular/core" ;
1+ import { Component , OnInit , OnDestroy , inject , Inject , HostListener } from "@angular/core" ;
22import { RouterOutlet } from "@angular/router" ;
33import { Subscription } from "rxjs" ;
44import { BlogInfo } from "./models/blog-info" ;
55import { BlogService } from "./services/blog.service" ;
66import { ThemeService } from "./services/theme.service" ;
7- import { DOCUMENT } from "@angular/common" ;
7+ import { DOCUMENT , ViewportScroller } from "@angular/common" ;
88import { HeaderComponent } from "./components/header/header.component" ;
99import { FooterComponent } from "./components/footer/footer.component" ;
10+ import { ButtonModule } from "primeng/button" ;
1011
1112@Component ( {
1213 selector : "app-root" ,
1314 standalone : true ,
14- imports : [ RouterOutlet , HeaderComponent , FooterComponent ] ,
15+ imports : [ RouterOutlet , HeaderComponent , FooterComponent , ButtonModule ] ,
1516 templateUrl : "./app.component.html" ,
1617 styleUrl : "./app.component.scss" ,
1718} )
@@ -23,8 +24,10 @@ export class AppComponent implements OnInit, OnDestroy {
2324 themeService : ThemeService = inject ( ThemeService ) ;
2425 blogService : BlogService = inject ( BlogService ) ;
2526 private querySubscription ?: Subscription ;
27+ showScrollButton : boolean = false ;
28+ private readonly scroller = inject ( ViewportScroller ) ;
2629
27- constructor ( @Inject ( DOCUMENT ) private document : Document ) { }
30+ constructor ( @Inject ( DOCUMENT ) private document : Document ) { }
2831
2932 ngOnInit ( ) : void {
3033 this . blogURL = this . blogService . getBlogURL ( ) ;
@@ -52,6 +55,20 @@ export class AppComponent implements OnInit, OnDestroy {
5255 } ) ;
5356 }
5457
58+ @HostListener ( 'window:scroll' , [ ] )
59+ onWindowScroll ( ) {
60+ const yOffset = window . pageYOffset || document . documentElement . scrollTop ;
61+ if ( yOffset > 300 ) {
62+ this . showScrollButton = true ;
63+ } else {
64+ this . showScrollButton = false ;
65+ }
66+ }
67+
68+ scrollToTop ( ) {
69+ this . scroller . scrollToPosition ( [ 0 , 0 ] ) ;
70+ }
71+
5572 ngOnDestroy ( ) : void {
5673 this . querySubscription ?. unsubscribe ( ) ;
5774 }
0 commit comments