tol*_*eza 6 scroll header sticky typescript angular
在Angular 4应用程序中向下滚动时,我在制作粘性标头时遇到问题。无法检测到滚动事件。
标题放置在布局组件中,而我要滚动的内容放置在路由组件中。可能是问题所在吗?
这是我实现的代码。
在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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
在layout.component.html中
<div [class.fixed]="navIsFixed" class="header">
Run Code Online (Sandbox Code Playgroud)
我只是遇到了同样的问题,我所要做的就是确保组件元素实际上是滚动的并且它具有overflow带有值的属性scroll或auto.
否则就是这样:
@HostListener('scroll')
public asd(): void {
console.log('scrolling');
}
Run Code Online (Sandbox Code Playgroud)
这应该给你滚动事件:
@HostListener('scroll', ['$event'])
onScroll(event) {
...
}
Run Code Online (Sandbox Code Playgroud)
或者
<div (scroll)="onScroll($event)"
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9765 次 |
| 最近记录: |