Having trouble creating a sticky header that stays fixed when scrolling down in an Angular 4 application. The scroll event is not being detected.
The header is located in the layout component, while the content I want to be scrollable is placed in the routes component. Could this arrangement be causing the issue?
Here is the implemented code:
In layout.component.ts
import { Component, OnInit, HostListener, Inject } from '@angular/core';
import { DOCUMENT } from "@angular/platform-browser";
@Component({
selector: 'app-layout',
templateUrl: './layout.component.html',
styleUrls: ['./layout.component.css']
})
export class LayoutComponent implements OnInit {
public navIsFixed: boolean = false;
constructor(public router: Router, @Inject(DOCUMENT) private document: any) { }
@HostListener('window:scroll', [ ])
onWindowScroll(){
const number = window.pageYOffset ||
document.documentElement.scrollTop ||
document.body.scrollTop || 0;
if (number > 50) {
this.navIsFixed = true;
} else if (this.navIsFixed && number < 10) {
this.navIsFixed = false;
}
}
}
In layout.component.html
<div [class.fixed]="navIsFixed" class="header">