Mic*_*xon 6 javascript typescript angular-material angular-flex-layout angular
我使用此处提供的示例来设置响应式导航栏
https://theinfogrid.com/tech/developers/angular/responsive-navbar-angular-flex-layout/
我的代码看起来非常相似
<div style="height: 85vh;">
<mat-toolbar color="primary" mat-scroll-shrink>
<span>{{title}}</span>
<span class="example-spacer"></span>
<div fxShow="true" fxHide.lt-md="true">
<!-- The following menu items will be hidden on both SM and XS screen sizes -->
<a href="#" mat-button>Home</a>
<a href="#" mat-button>About</a>
<a href="#" mat-button>Services</a>
<a href="#" mat-button>Portfolio</a>
<a href="#" mat-button>Start</a>
<a href="#" mat-button>FAQ</a>
<a href="#" mat-button>Blog</a>
<a href="#" mat-button>Contact</a>
</div>
<div fxShow="true" fxHide.gt-sm="true">
<a href="#" (click)="sidenav.open()">Show Side Menu</a>
</div>
</mat-toolbar>
<mat-sidenav-container fxFlexFill class="example-container">
<mat-sidenav #sidenav fxLayout="column">
<div fxLayout="column">
<a (click)="sidenav.close()" href="#" mat-button>Close</a>
<a href="#" mat-button>Home</a>
<a href="#" mat-button>About</a>
<a href="#" mat-button>Services</a>
<a href="#" mat-button>Portfolio</a>
<a href="#" mat-button>Start</a>
<a href="#" mat-button>FAQ</a>
<a href="#" mat-button>Blog</a>
<a href="#" mat-button>Contact</a>
</div>
</mat-sidenav>
<mat-sidenav-content fxFlexFill>
<p>Demoing some content to make this thing scroll</p>
<p>Demoing some content to make this thing scroll</p>
<p>Demoing some content to make this thing scroll</p>
<p>Demoing some content to make this thing scroll</p>
<p>Demoing some content to make this thing scroll</p>
<p>Demoing some content to make this thing scroll</p>
<p>Demoing some content to make this thing scroll</p>
<p>Demoing some content to make this thing scroll</p>
<p>Demoing some content to make this thing scroll</p>
<p>Demoing some content to make this thing scroll</p>
<p>Demoing some content to make this thing scroll</p>
<p>Demoing some content to make this thing scroll</p>
<p>Demoing some content to make this thing scroll</p>
<p>Demoing some content to make this thing scroll</p>
<p>Demoing some content to make this thing scroll</p>
<p>Demoing some content to make this thing scroll</p>
<p>Demoing some content to make this thing scroll</p>
<p>Demoing some content to make this thing scroll</p>
<p>Demoing some content to make this thing scroll</p>
<p>Demoing some content to make this thing scroll</p>
<p>Demoing some content to make this thing scroll</p>
<p>Demoing some content to make this thing scroll</p>
<p>Demoing some content to make this thing scroll</p>
</mat-sidenav-content>
</mat-sidenav-container>
</div>
Run Code Online (Sandbox Code Playgroud)
我想要发生的是当我向下滚动时,mat-toolbar会缩小,这在很多站点都很常见,比如这个:
我不会发布其余的角度5代码,只需按照示例重新创建 - 它非常快.
我在这里查看了材料网站
https://material.angular.io/components/toolbar/overview
但是关于如何添加它没有太多解释,我对这些东西很新.有没有人知道如何自定义这个以使工具栏缩小,基于滚动?
Kim*_*ern 11
在ScrollDispatchModule与角CDK V7弃用.使用ScrollingModule来代替.
我创建了一个Stackblitz,其工具栏在向下滚动时会缩小.
CdkScrollDispatcher服务来响应滚动事件ScrollDispatchModule模块中.import {ScrollDispatchModule} from '@angular/cdk/scrolling';
Run Code Online (Sandbox Code Playgroud)
cdkScrollable这里mat-sidenav-content. <mat-sidenav-content fxFlexFill cdkScrollable>
Run Code Online (Sandbox Code Playgroud)
ngOnInit组件的事件,获取scrollTop位置并设置一个标志,如果它大于某个阈值:private readonly SHRINK_TOP_SCROLL_POSITION = 50;
shrinkToolbar = false;
constructor(private scrollDispatcher: ScrollDispatcher,
private ngZone: NgZone) { }
ngOnInit() {
this.scrollDispatcher.scrolled()
.pipe(
map((event: CdkScrollable) => event.getElementRef().nativeElement.scrollTop)
)
.subscribe(scrollTop => this.ngZone.run(() => this.shrinkToolbar = scrollTop > this.SHRINK_TOP_SCROLL_POSITION ? true : false));
}
Run Code Online (Sandbox Code Playgroud)
你需要运行它,ngZone因为默认情况下scrolled(),ScrollDispatcher它们的事件在Angular之外运行.没有它,ChangeDetection将无法运行,您的模板也不会更新.
<mat-toolbar color="primary" [ngClass]="{'shrink-toolbar': shrinkToolbar}">
Run Code Online (Sandbox Code Playgroud)
.shrink-toolbar {
height: 32px;
}
Run Code Online (Sandbox Code Playgroud)
在官方文档中查找有关滚动服务的更多信息.
| 归档时间: |
|
| 查看次数: |
4818 次 |
| 最近记录: |