最佳答案
无论您是否使用 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;
}
关于html - 如何使用 Angular Material 创建粘性 header ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54033364/