简体   繁体   English

如何使可切换的 mat-sidenav 推送在 mat-sidenav-content 中也固定为 header

[英]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.

相关问题 如何固定 mat-toolbar 和 mat-sidenav 并且在 mat-sidenav-content 内只有一个滚动条? - How to pin mat-toolbar and mat-sidenav and only have a scrollbar inside mat-sidenav-content? 如何让 mat-sidenav 占据 mat-toolbar 下方的整个垂直空间? - How can I make mat-sidenav take up entire vertical space underneath mat-toolbar? 让内容流出<mat-sidenav>而不是为它创建滚动条</mat-sidenav> - Let content flow outside of <mat-sidenav> instead of creating scrollbar for it 除非设置了内容高度,否则不会显示 Angular mat-sidenav - Angular mat-sidenav is not showing unless content height is set 动画期间的 mat-sidenav 滚动条可见性 - mat-sidenav scrollbar visibility during animation 垫子工具栏的宽度与垫子侧面导航不匹配 - mat-toolbar not match width with mat-sidenav 由 Angular 中的子组件覆盖父样式(mat-sidenav) - Override parent style by a child compoent in Angular (mat-sidenav) Angular:mat-sidenav 在调整大小时忽略自动调整大小或模式 - Angular: mat-sidenav ignores autosize or mode on resize Angular 6,7 如何将默认主题颜色应用于 mat-sidenav 背景? - Angular 6,7 How to apply default theme color to mat-sidenav background? Angular 材质 - 禁用 mat-sidenav 过渡 animation 但保留儿童动画? - Angular Material - Disable mat-sidenav transition animation but keep children animations?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM