答案 0 :(得分:1)
无论是否使用Angular Material,粘性标题的基本实现都是相同的-观察页面的滚动位置并根据需要隐藏或显示第二个标题。这是一个使用Angular Material工具栏组件作为标题的粗略示例:
<div style="min-height: 150vh;"> <!-- Set minimum height to force a scrollbar -->
<mat-toolbar color="primary">
<mat-toolbar-row>
<span>Header 1</span>
</mat-toolbar-row>
</mat-toolbar>
<mat-toolbar color="secondary" style="position: fixed; top: 0;" *ngIf="scrolled">
<mat-toolbar-row>
<span>Header 2</span>
</mat-toolbar-row>
</mat-toolbar>
</div>
在.ts文件中:
scrolled: false;
@HostListener('window:scroll', [])
onWindowScroll() {
// Depending on the desired effect, you should probably only show the second header
// if you've scrolled past the first header's height
this.scrolled = window.pageYOffset > 48;
}