[英]Angular 5 Stop background scrolling when mat-sidenav-container displayed
I'm creating a new Angular 5
site that has a mat-sidenav
but i have an issue with it.我正在创建一个新的Angular 5
站点,它有一个mat-sidenav
但我有一个问题。
My mat-sidenav-container
sits under my position: fixed
main menu which is fine but if the page has a vertical scroll, if the mat-sidenav
is displayed it too scrolls.我的mat-sidenav-container
位于我的position: fixed
主菜单,这很好,但如果页面有垂直滚动,如果显示mat-sidenav
,它也会滚动。
What i'm after is to stop the back content scrolling if the mat-sidenav
is displayed but i don't know the CSS
to do this as i have tried all the position
我所追求的是在显示mat-sidenav
停止后面的内容滚动,但我不知道CSS
来执行此操作,因为我已经尝试了所有position
HTML HTML
<mat-toolbar>
<mat-icon class="subMenuIcon" (click)="sidenav.toggle()" title="Click to open the sub-menu.">reorder</mat-icon>
<img src="./assets/images/Crest.jpg">LEARNING SITE
<div class="emptySpace"></div>
<mat-icon class="loggedInIcon">person</mat-icon>
<span style="font-size: 15px">USERNAME</span>
</mat-toolbar>
<mat-sidenav-container>
<mat-sidenav #sidenav mode="over" opened="false">
<div class="closeButton">
<mat-icon (click)="sidenav.toggle()" title="Click to close the sub-menu.">clear</mat-icon>
</div>
<a (click)="sidenav.toggle()" routerLink="/">
<mat-icon>person</mat-icon>
Dashboard
</a>
<a (click)="sidenav.toggle()" routerLink="search">
<mat-icon>search</mat-icon>
Search
</a>
</mat-sidenav>
</mat-sidenav-container>
<router-outlet></router-outlet>
CSS CSS
mat-sidenav {
background-color: gray;
margin-top: 64px;
color: #ffffff;
transition: 0.5s;
padding: 7px;
}
mat-sidenav a {
margin-top: 15px;
margin-bottom: 15px;
font-size: 17px;
color: #ffffff;
display: block;
}
mat-sidenav a:hover {
color: #c41230;
cursor: pointer;
}
.closeButton {
text-align: right;
margin-bottom: 15px;
cursor: pointer;
}
.mat-drawer-container {
position: static;
}
/* Moves the side-nav to below the main menu */
.mat-drawer-backdrop {
margin-top: 64px !important;
}
在 mat-sidenav 中添加fixedInViewport="true"
可能会解决该问题,同时添加[style.marginTop.px]="56"
METHOD 1方法一
In your component (where side-nav is located), add a hostListener, where you stop the propagation of your scroll event, then only your sidenav will scroll, not the background.在您的组件(side-nav 所在的位置)中,添加一个 hostListener,在那里您停止滚动事件的传播,然后只有您的 sidenav 会滚动,而不是背景。
@HostListener("window:scroll", ["$event"])
onWindowScroll(event) {
// prevent the background from scrolling when the dialog is open.
event.stopPropagation();
}
METHOD 2方法二
You can add a scroll listener to your sidenav, and stop the propagation there.您可以向 sidenav 添加滚动侦听器,并在那里停止传播。 add a method in your component, and use it in your html:在您的组件中添加一个方法,并在您的 html 中使用它:
in the html, add: (scroll)="onSideNavScroll($event)"
to your side-nav like this:在 html 中,像这样添加: (scroll)="onSideNavScroll($event)"
到你的侧边导航:
<mat-sidenav-container (scroll)="onSideNavScroll($event)">
<mat-sidenav #sidenav mode="over" opened="false">
<div class="closeButton">
<mat-icon (click)="sidenav.toggle()" title="Click to close the sub-menu.">clear</mat-icon>
</div>
<a (click)="sidenav.toggle()" routerLink="/">
<mat-icon>person</mat-icon>
Dashboard
</a>
<a (click)="sidenav.toggle()" routerLink="search">
<mat-icon>search</mat-icon>
Search
</a>
</mat-sidenav>
</mat-sidenav-container>
In your component, add this function:在您的组件中,添加此功能:
onSideNavScroll(event){ event.stopPropagation() }
Managed to do it with a bit of CSS
.设法用一些CSS
来做到这一点。 not bother that the back content scrolls but now my side-nav
and backdrop do not move不打扰后面的内容滚动但现在我的side-nav
和背景不移动
CSS Used使用的 CSS
.mat-drawer:not(.mat-drawer-side) {
position: fixed;
}
.mat-drawer-backdrop {
position: fixed !important;
}
由于<mat-sidenav #sidenav>
有一个模板引用变量 (#sidenav), <mat-sidenav-container>
可以使用条件样式固定(就位),从而在打开 sidnav 时有效地移除背景滚动。
<mat-sidenav-container [ngStyle] = "{ 'position' : sidenav.opened ? 'fixed' : 'relative'}">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.