如果只想看结果,请到答案末尾
要做的主要事情是将切换按钮与sidenav
.
HTML
<p><button [ngClass]="{'button':true,'open':opened}" (click)="toggle()">Open</button></p>
<mat-sidenav-container class="sidenav" (backdropClick)="close()">
<mat-sidenav #sidenav (keydown.escape)="close()" disableClose position="end">
<p>Sidenav content</p>
</mat-sidenav>
<mat-sidenav-content>
Page content with "over" sidenav
</mat-sidenav-content>
</mat-sidenav-container>
在这里,您将按钮从 中取出sidenav
,类允许您首先将按钮放置在您想要的位置 - 使用button
类 - 然后在您打开或关闭侧导航时移动它 - 使用open
类。
和(backdropClick)="close()"
是(keydown.escape)="close()"
为了确保我们移动切换按钮,即使我们不关闭sidenav
它。
所以这个的风格是这样的
CSS
.button{
position:absolute;
width:40px;
height:40px;
background-color: green;
color:black;
border-top-left-radius:5px;
border-bottom-left-radius:5px;
top: 50%;
right:0;
z-index: 2;
transform: translate3d(0,0,0);
transition:0.4s cubic-bezier(0.25, 0.8, 0.25, 1);
}
.open{
transform: translate3d(-200px,0,0);
transition:0.4s cubic-bezier(0.25, 0.8, 0.25, 1);
}
在transition
效果上,您会看到一个cubic-bezier(0.25, 0.8, 0.25, 1);
用于sidenav
显示其内容的转换。因此,在我们的按钮中使用了这种过渡,它将完美地适应sidenav
移动。
最后在你的打字稿文件上,你只需要切换你的功能sidenav
TS
@ViewChild('sidenav') sidenav: MatSidenav;
opened: boolean = false;
toggle(){
this.opened = !this.opened
if(this.opened){
this.sidenav.open();
}else{
this.sidenav.close();
}
}
close() {
this.opened = false;
this.sidenav.close();
}
在这里,您可以看到正在运行的 stackblitz。