11import { Component , OnInit , OnDestroy , inject , Inject } from "@angular/core" ;
22import { RouterOutlet } from "@angular/router" ;
3- import { Subscription } from "rxjs" ;
3+ import { Subscription , fromEvent } 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,6 +24,9 @@ export class AppComponent implements OnInit, OnDestroy {
2324 themeService : ThemeService = inject ( ThemeService ) ;
2425 blogService : BlogService = inject ( BlogService ) ;
2526 private querySubscription ?: Subscription ;
27+ private scrollEvntSub ?: Subscription ;
28+ enableScrollUp : boolean = false ;
29+ private readonly scroller = inject ( ViewportScroller ) ;
2630
2731 constructor ( @Inject ( DOCUMENT ) private document : Document ) { }
2832
@@ -50,9 +54,30 @@ export class AppComponent implements OnInit, OnDestroy {
5054 } ) ;
5155 }
5256 } ) ;
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 ) ;
5377 }
5478
5579 ngOnDestroy ( ) : void {
80+ this . scrollEvntSub ?. unsubscribe ( ) ;
5681 this . querySubscription ?. unsubscribe ( ) ;
5782 }
5883}
0 commit comments