[英]How to make a toggleable mat-sidenav push also a fixed header in the mat-sidenav-content
I'm using the mat-sidenav-container to create the desktop version of my page, but I want to make the mat-sidenav toggleable, which I've already managed to do.我正在使用 mat-sidenav-container 创建我的页面的桌面版本,但我想让 mat-sidenav 可以切换,我已经设法做到了。 The problem now is that I have a fixed header on the mat-sidenav-content, and that part is not being pushed when the mat-sidenav is displayed.现在的问题是,我在 mat-sidenav-content 上有一个固定的 header,当显示 mat-sidenav 时,该部分没有被推送。
For the mat-sidenav-content I'm using the css grid to divide this section into two parts (the fixed header and the main content)对于 mat-sidenav-content,我使用 css 网格将此部分分为两部分(固定 header 和主要内容)
Here's a stackblitz demo .这是一个 stackblitz 演示。
Do you have any idea how I can make the mat-sidenav push also the fixed header?你知道我怎样才能让 mat-sidenav 也推送固定的 header 吗?
Thanks.谢谢。
You have to remove these styles from .content__header :您必须从.content__header中删除这些 styles :
height: 12rem;
position: fixed;
left: 0;
and add position: sticky
to it.并添加position: sticky
它。 Because of the position: fixed
, the block could not be pushed to the side by the sidebar and because of the fixed height, it overflowed when the position: fixed
was removed.由于position: fixed
,块不能被侧边栏推到一边,由于固定高度,当position: fixed
被移除时它溢出。 The height was especially not needed, as you already specified it in your .content class with this: grid-template-rows: 12rem auto;
特别不需要高度,因为您已经在.content class 中指定了它: grid-template-rows: 12rem auto;
. . Changing it to position: sticky
makes the header still be there on scroll, but also be pushed to the side.将其更改为position: sticky
使 header 仍然在滚动条上,但也会被推到一边。 Important is that top: 0
is still there.重要的是top: 0
仍然存在。
The .content__header class should now look like this: .content__header class 现在应该如下所示:
.content__header {
background-color: #eee;
width: 100%;
position: sticky;
top: 0;
padding: 0.5rem 1rem;
z-index: 3;
}
In Angular 14 I tried the following mat-sidenav-container and it works.在 Angular 14 中,我尝试了以下 mat-sidenav-container 并且它有效。 Let me know how it goes for you.让我知道你的情况如何。
.app-sidenav-container { position: absolute; top: 0; bottom: 0; left: 0; right: 0; background: #eee; }.app-sidenav-content { display: flex; height: 100%; align-items: center; justify-content: center; }.app-sidenav { padding: 50px; }
<mat-sidenav-container class="app-sidenav-container"> <mat-sidenav #sidenav mode="side" class="app-sidenav"> <mat-toolbar color="primary"> Contacts </mat-toolbar> <mat-list role="list"> <mat-list-item role="listitem">Item 1</mat-list-item> <mat-list-item role="listitem">Item 2</mat-list-item> <mat-list-item role="listitem">Item 3</mat-list-item> </mat-list> </mat-sidenav> <div class="app-sidenav-content" > <app-toolbar></app-toolbar> <router-outlet></router-outlet> <button type="button" mat-button (click)="(sidenav.opened)? sidenav.close(): sidenav.open()"> Open Sidenav </button> </div> </mat-sidenav-container>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.