Skip to content

Commit 6c2f3d5

Browse files
committed
refactored code due to PR request comments:
1.renaming  variable : enableScrollUp-showScrollButton method : goPageTop-scrollToTop 2. hostlistener used instead of rxjs scroll evnt sub  3.window.pageYoffet or document.scrollTop value is used instead of cal scroll percentage. 4.space before ButtonModule
1 parent 4288fcd commit 6c2f3d5

File tree

2 files changed

+17
-25
lines changed

2 files changed

+17
-25
lines changed

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +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>
6+
@if (showScrollButton) {
7+
<p-button class="pageUpButton" (click)="scrollToTop()" icon="pi pi-angle-up" [rounded]="true" size="small"></p-button>
88
}

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

+15-23
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import { Component, OnInit, OnDestroy, inject, Inject } from "@angular/core";
1+
import { Component, OnInit, OnDestroy, inject, Inject, HostListener } from "@angular/core";
22
import { RouterOutlet } from "@angular/router";
3-
import { Subscription, fromEvent } from "rxjs";
3+
import { Subscription } from "rxjs";
44
import { BlogInfo } from "./models/blog-info";
55
import { BlogService } from "./services/blog.service";
66
import { ThemeService } from "./services/theme.service";
@@ -12,7 +12,7 @@ import { ButtonModule } from "primeng/button";
1212
@Component({
1313
selector: "app-root",
1414
standalone: true,
15-
imports: [RouterOutlet, HeaderComponent, FooterComponent,ButtonModule],
15+
imports: [RouterOutlet, HeaderComponent, FooterComponent, ButtonModule],
1616
templateUrl: "./app.component.html",
1717
styleUrl: "./app.component.scss",
1818
})
@@ -24,11 +24,10 @@ export class AppComponent implements OnInit, OnDestroy {
2424
themeService: ThemeService = inject(ThemeService);
2525
blogService: BlogService = inject(BlogService);
2626
private querySubscription?: Subscription;
27-
private scrollEvntSub?: Subscription;
28-
enableScrollUp: boolean=false;
27+
showScrollButton: boolean = false;
2928
private readonly scroller = inject(ViewportScroller);
3029

31-
constructor(@Inject(DOCUMENT) private document: Document) {}
30+
constructor(@Inject(DOCUMENT) private document: Document) { }
3231

3332
ngOnInit(): void {
3433
this.blogURL = this.blogService.getBlogURL();
@@ -54,30 +53,23 @@ export class AppComponent implements OnInit, OnDestroy {
5453
});
5554
}
5655
});
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-
})
6556
}
6657

67-
goPageTop() {
68-
this.scroller.scrollToPosition([0, 0]);
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+
}
6966
}
7067

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);
68+
scrollToTop() {
69+
this.scroller.scrollToPosition([0, 0]);
7770
}
7871

7972
ngOnDestroy(): void {
80-
this.scrollEvntSub?.unsubscribe();
8173
this.querySubscription?.unsubscribe();
8274
}
8375
}

0 commit comments

Comments
 (0)