1
1
import { Component , OnInit , OnDestroy , inject , Inject } from "@angular/core" ;
2
2
import { RouterOutlet } from "@angular/router" ;
3
- import { Subscription } from "rxjs" ;
3
+ import { Subscription , fromEvent } 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,6 +24,9 @@ export class AppComponent implements OnInit, OnDestroy {
23
24
themeService : ThemeService = inject ( ThemeService ) ;
24
25
blogService : BlogService = inject ( BlogService ) ;
25
26
private querySubscription ?: Subscription ;
27
+ private scrollEvntSub ?: Subscription ;
28
+ enableScrollUp : boolean = false ;
29
+ private readonly scroller = inject ( ViewportScroller ) ;
26
30
27
31
constructor ( @Inject ( DOCUMENT ) private document : Document ) { }
28
32
@@ -50,9 +54,30 @@ export class AppComponent implements OnInit, OnDestroy {
50
54
} ) ;
51
55
}
52
56
} ) ;
57
+
58
+ this . scrollEvntSub = fromEvent ( this . document , 'scroll' )
59
+ . subscribe ( ( ) => {
60
+ if ( this . calculatePositionScroll ( ) > 50 )
61
+ this . enableScrollUp = true ;
62
+ else
63
+ this . enableScrollUp = false ;
64
+ } )
65
+ }
66
+
67
+ goPageTop ( ) {
68
+ this . scroller . scrollToPosition ( [ 0 , 0 ] ) ;
69
+ }
70
+
71
+ calculatePositionScroll ( ) : number {
72
+ var scrollTop = this . document . documentElement . scrollTop ;
73
+ var docHeight = this . document . documentElement . scrollHeight ;
74
+ var winHeight = this . document . documentElement . clientHeight ;
75
+ var scrollPercent = ( scrollTop ) / ( docHeight - winHeight ) ;
76
+ return Math . round ( scrollPercent * 100 ) ;
53
77
}
54
78
55
79
ngOnDestroy ( ) : void {
80
+ this . scrollEvntSub ?. unsubscribe ( ) ;
56
81
this . querySubscription ?. unsubscribe ( ) ;
57
82
}
58
83
}
0 commit comments