I want to make a change to the menu position so that it becomes fixed when it reaches the top of the page. Here is my approach:
import { Component, OnInit, Inject, HostListener } from '@angular/core';
import {Location} from '@angular/common';
import { DOCUMENT } from "@angular/platform-browser";
....
export class DashboardComponent implements OnInit {
constructor(private _location: Location, @Inject(DOCUMENT) private doc: Document) { }
public fixed: boolean = false;
@HostListener("window:scroll", [])
onWindowScroll() {
let num = this.doc.body.scrollTop;
console.log("scroll top" , this.doc.body.scrollTop)
if ( num > 50 ) {
this.fixed = true;
} else if (this.fixed && num < 5) {
this.fixed = false;
}
}
In HTML
<div [class.menus]="fixed">
<div class="left-menu ">
<app-left-menu></app-left-menu>
</div>
<div class="second-menu" >
<app-second-menu (display)="onDisplay($event)" [expanded]=expanded ></app-second-menu>
</div>
</div>
CSS
.menus{
position: fixed;
}
The issue I am facing is that the scrollTop
value is not changing. Even when I do
console.log(this.doc.body.scrollTop)
and scroll, the value remains at 0.