1
- import { Component , OnInit , OnDestroy , inject , Inject } from "@angular/core" ;
1
+ import { Component , OnInit , OnDestroy , inject , Inject , HostListener } from "@angular/core" ;
2
2
import { RouterOutlet } from "@angular/router" ;
3
3
import { Subscription } from "rxjs" ;
4
4
import { BlogInfo } from "./models/blog-info" ;
5
5
import { BlogService } from "./services/blog.service" ;
6
6
import { ThemeService } from "./services/theme.service" ;
7
- import { DOCUMENT } from "@angular/common" ;
7
+ import { DOCUMENT , ViewportScroller } from "@angular/common" ;
8
8
import { HeaderComponent } from "./components/header/header.component" ;
9
9
import { FooterComponent } from "./components/footer/footer.component" ;
10
+ import { ButtonModule } from "primeng/button" ;
10
11
11
12
@Component ( {
12
13
selector : "app-root" ,
13
14
standalone : true ,
14
- imports : [ RouterOutlet , HeaderComponent , FooterComponent ] ,
15
+ imports : [ RouterOutlet , HeaderComponent , FooterComponent , ButtonModule ] ,
15
16
templateUrl : "./app.component.html" ,
16
17
styleUrl : "./app.component.scss" ,
17
18
} )
@@ -23,8 +24,10 @@ export class AppComponent implements OnInit, OnDestroy {
23
24
themeService : ThemeService = inject ( ThemeService ) ;
24
25
blogService : BlogService = inject ( BlogService ) ;
25
26
private querySubscription ?: Subscription ;
27
+ showScrollButton : boolean = false ;
28
+ private readonly scroller = inject ( ViewportScroller ) ;
26
29
27
- constructor ( @Inject ( DOCUMENT ) private document : Document ) { }
30
+ constructor ( @Inject ( DOCUMENT ) private document : Document ) { }
28
31
29
32
ngOnInit ( ) : void {
30
33
this . blogURL = this . blogService . getBlogURL ( ) ;
@@ -52,6 +55,20 @@ export class AppComponent implements OnInit, OnDestroy {
52
55
} ) ;
53
56
}
54
57
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
+
55
72
ngOnDestroy ( ) : void {
56
73
this . querySubscription ?. unsubscribe ( ) ;
57
74
}
0 commit comments