简体   繁体   English

如何将“mat-toolbar”放在“mat-sidenav”Angular Material 5 的顶部

[英]How to place "mat-toolbar" on top of "mat-sidenav" Angular Material 5

I'm writing here since I have a question about Mat Toolbar and Mat-sidenav from Angular Material.我写在这里是因为我有一个关于 Angular Material 的 Mat Toolbar 和 Mat-sidenav 的问题。 I'm trying to get the Sidenav to go under the toolbar and the toolbar always occupies the top part, something like this:我试图让 Sidenav 进入工具栏下方,并且工具栏始终占据顶部,如下所示:

Example例子

Here´s my code:这是我的代码:

 <mat-sidenav-container class="sidenav-container" autosize> <mat-sidenav #drawer class="sidenav" fixedInViewport="true" [attr.role]="(isHandset$ | async) ? 'dialog' : 'navigation'" [mode]="(isHandset$ | async) ? 'side' : 'push'" [opened]="(isHandset$ | async)"> <mat-toolbar class="fixed-header"> <img class="logooTest" src="data:image/gif;base64,test"/> </mat-toolbar> <mat-nav-list> <mat-list-item> <a routerLink="/dashboard">Test</a> <mat-icon mat-list-icon>home</mat-icon> </mat-list-item> <mat-list-item> <a routerLink="/dashboard">Test</a> <mat-icon mat-list-icon>home</mat-icon> </mat-list-item> <mat-list-item> <a routerLink="/test">Test</a> <mat-icon mat-list-icon>tune</mat-icon> </mat-list-item> <mat-list-item> <a routerLink="/#">Test</a> <mat-icon mat-list-icon>settings</mat-icon> </mat-list-item> <mat-list-item> <a routerLink="/#">Test</a> <mat-icon mat-list-icon>layers</mat-icon> </mat-list-item> <mat-list-item (click)="showSubmenu = !showSubmenu" class="parent"> <span class="full-width" *ngIf="isExpanded || isShowing">Test dropdown</span> <mat-icon mat-list-icon>flash_on</mat-icon> <mat-icon class="menu-button" [ngClass]="{'rotated' : showSubmenu}" *ngIf="isExpanded || isShowing">expand_more</mat-icon> </mat-list-item> <div class="submenu" [ngClass]="{'expanded' : showSubmenu}" *ngIf="isShowing || isExpanded"> <mat-list-item> <a routerLink="/#">Test</a> </mat-list-item> <mat-list-item> <a routerLink="/#">Test</a> </mat-list-item> <h2 matSubheader><mat-icon>account_balance</mat-icon> Test</h2> <mat-list-item (click)="showSubSubMenu = !showSubSubMenu" class="parent"> <span class="full-width" *ngIf="isExpanded || isShowing">Test</span> <mat-icon class="menu-button" [ngClass]="{'rotated' : showSubSubMenu}" *ngIf="isExpanded || isShowing">expand_more</mat-icon> </mat-list-item> <div class="submenu" [ngClass]="{'expanded' : showSubSubMenu}" *ngIf="isShowing || isExpanded"> <mat-list-item> <a routerLink="/test">Test</a> </mat-list-item> <mat-list-item> <a routerLink="/#">Test</a> </mat-list-item> <h2 matSubheader><mat-icon>card_travel</mat-icon> Test</h2> <mat-list-item> <a routerLink="/#">Test</a> </mat-list-item> <mat-list-item> <a routerLink="/#">Test</a> </mat-list-item> </div> </div> </mat-nav-list> </mat-sidenav> <mat-sidenav-content> <mat-toolbar color="primary" class="mat-elevation-z5"> <button type="button" aria-label="Toggle sidenav" mat-icon-button (click)="drawer.toggle()"> <mat-icon aria-label="Side nav toggle icon">menu</mat-icon> </button> <span class="spacer"></span> <div class="navigation"> <a class="button" href=""> <mat-icon class="logoutIcon">exit_to_app</mat-icon> <div class="logout"><span class="aligned-with-icon">Salir</span></div> </a> </div> </mat-toolbar> </mat-sidenav-content> </mat-sidenav-container>

I'm using the version 5.6.0 of Angular and Angular Material.我正在使用 Angular 和 Angular Material 的 5.6.0 版本。 I tried using CSS and also changing the order of the structure of the code, but this last one simply gave me errors and more errors;我尝试使用 CSS 并更改代码结构的顺序,但最后一个只是给了我错误和更多错误; preventing the application from running.阻止应用程序运行。

Anything you put inside mat-sidenav-content appears beside the menu.您放入mat-sidenav-content任何内容都会出现在菜单旁边 The basic layout structure for toolbar above sidenav menu and content is: sidenav 菜单和内容上方工具栏的基本布局结构是:

<mat-toolbar>...</mat-toolbar>
<mat-sidenav-container>...</mat-sidenav-container>

Here's an example on StackBlitz .这是StackBlitz上的一个示例。

对我来说,只需像 G. Tranter 一样更改 html 并将 mat- fixedInViewport中的 fixedInViewport 从true设置为false

A very simple way to do this is to add z-index to 10 in your mat-toolbar.一个非常简单的方法是在 mat-toolbar 中将 z-index 添加到 10。 Here is the css for that.这是为此的CSS。

.mat-toolbar-example{
  position: fixed;
  z-index:10;
}
<mat-toolbar class="mat-toolbar-example"></mat-toolbar>
.example-mat-toolbar {
    position: sticky;
    position: -webkit-sticky; /* For macOS/iOS Safari */
    top: 0; 
    z-index: 10;
}
.example-sidenav-container {
    position: relative;
    height: 100%;
    display: block;
    transform: inherit;
}

If you are using Angular 6 for your project you can easily generate a boilerplate for Mat-toolbar and Mat-sidenav by running the following command via cli:如果您在项目中使用 Angular 6,您可以通过 cli 运行以下命令轻松地为 Mat-toolbar 和 Mat-sidenav 生成样板:

ng generate @angular/material:materialNav --name myNav

Once you make the sidenav component visible, you should be see the following result使 sidenav 组件可见后,您应该会看到以下结果

Mat Sidenav component Mat Sidenav 组件

Your pattern must be like this in the html:您的模式在 html 中必须是这样的:

<mat-toolbar color="primary">
   ...
</mat-toolbar>

<mat-sidenav-container>
  <mat-sidenav>
     ...
  </mat-sidenav>
  <mat-sidenav-content>
     ...
    <router-outlet></router-outlet>
  </mat-sidenav-content>
</mat-sidenav-container>

Then style it with your css like this:然后用你的 CSS 来设置它的样式,如下所示:

.mat-toolbar {
  position: sticky;
  top: 0;
  z-index: 99;
}

make sure you z-index is about your other content that scrolls under it.确保您的 z-index 是关于您在其下滚动的其他内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 mat-sidenav 放在 mat-toolbar 下 - How to place mat-sidenav under the mat-toolbar 如何固定 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-toolbar 和 sidenav 保持在顶部? - How to stay mat-toolbar and sidenav on top in side mode (angular material)? 垫子工具栏的宽度与垫子侧面导航不匹配 - mat-toolbar not match width with mat-sidenav 强制 angular mat-toolbar 和 mat-sidenav 准确占用 100% 的可用垂直空间 - Force angular mat-toolbar and mat-sidenav to take up exactly 100% of the available vertical space 如何让 mat-sidenav 占据 mat-toolbar 下方的整个垂直空间? - How can I make mat-sidenav take up entire vertical space underneath mat-toolbar? mat-toolbar,mat-sidenav和mat-sidenav-container未知 - mat-toolbar, mat-sidenav and mat-sidenav-container is not a known element Angular Material 2 - 如何将 mat-toolbar 和 mat-tabs 锁定到顶部 - Angular Material 2 - How to lock mat-toolbar and mat-tabs to the top 如何制作粘性角材料垫工具栏? - How to make a sticky Angular Material mat-toolbar? Angular2 + Material:尽管 mat-elevation-z*,mat-toolbar 不会在 mat-sidenav-container 上投下阴影 - Angular2 + Material: mat-toolbar casts no shadow over mat-sidenav-container despite mat-elevation-z*
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM