I would change menu position to fixed when menu reach top of page. here's how I proceed.
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 problem is that scrollTop
is not getting changed. when I do console.log(this.doc.body.scrollTop)
and scroll, value is 0 and don't change.
Most likely the body is not not the scroll container, the HTML element is
Try document.documentElement.scrollTop
In browsers the document.documentElement
is a reference to the root HTML element
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.