我在使用Angular 4應用程序向下滾動時遇到粘貼頭問題。無法檢測到滾動事件。使用@HostListener不工作的窗口滾動事件
標題放置在佈局組件中,我想要滾動的內容放置在路由組件中。這可能是問題嗎?
這是我實施的代碼。
在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;
}
}
}
在layout.component.html
<div [class.fixed]="navIsFixed" class="header">
如果你在頂級組件定義的呢? app.component.ts(我假設你的路由器出口在那裏......) – Carsten
你找到了解決方案嗎?我有同樣的問題@tolceza – natdico