Skip to content

Commit 4288fcd

Browse files
committed
feat(component): add new feature button : returns the user to the beginning of the page
1 parent e572d7c commit 4288fcd

File tree

3 files changed

+38
-3
lines changed

3 files changed

+38
-3
lines changed

angular-primeng-app/src/app/app.component.html

+3
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
<router-outlet />
44
</div>
55
<app-footer />
6+
@if (enableScrollUp) {
7+
<p-button class="pageUpButton" (click)="goPageTop()" icon="pi pi-angle-up" [rounded]="true" size="small"></p-button>
8+
}

angular-primeng-app/src/app/app.component.scss

+7
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,10 @@
55
min-height: 74vh;
66
width: 100%;
77
}
8+
9+
.pageUpButton {
10+
position: fixed;
11+
bottom: 10px;
12+
right: 14px;
13+
}
14+

angular-primeng-app/src/app/app.component.ts

+28-3
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,18 @@
11
import { Component, OnInit, OnDestroy, inject, Inject } from "@angular/core";
22
import { RouterOutlet } from "@angular/router";
3-
import { Subscription } from "rxjs";
3+
import { Subscription, fromEvent } from "rxjs";
44
import { BlogInfo } from "./models/blog-info";
55
import { BlogService } from "./services/blog.service";
66
import { ThemeService } from "./services/theme.service";
7-
import { DOCUMENT } from "@angular/common";
7+
import { DOCUMENT, ViewportScroller } from "@angular/common";
88
import { HeaderComponent } from "./components/header/header.component";
99
import { 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

Comments
 (0)