I'm trying to fix a div when scrolling down; for that, I began with the below code, I am facing some weird issue that prevents me from detecting the scrolling behavior. On the below code console.log('bigger than 50') does not get printed when scrolling down.
Any ideas why I can't detect the scroll event?
@Component({
templateUrl: 'outlet.html',
selector: "app-layout-nav"
})
export class place {
constructor(@Inject(DOCUMENT) private document: Document) {}
@HostListener("window:scroll", [])
onWindowScroll() {
let number = this.document.body.scrollTop;
if (number > 50) {
console.log('bigger than 50')
this.navIsFixed = true;
} else if (this.navIsFixed && number < 10) {
console.log('lower than 50')
this.navIsFixed = false;
}
}
}
Outlet.html
<div>
<div id="wrap" [class.fixed]="navIsFixed">
This should be fixed when it reaches a certain level</div>
<div>The rest of the long page should show here</div>
</div>
window:scrollis only for when the whole page is scrolled. We would need to see the HTML to and a pointer to the element that is supposed to scroll.window:scrollas the user should scroll the window, and a particulardivon that window will befixedafter reaching a certain level of scrolling. Please correct me if I am wrong.@HostListener()should capture the scroll events, if it doesn't then not.@HostListener()does not capture the scroll events