当页面标题在侧导航内容中不可见时,在工具栏中显示文本

我正在做一个有角度的项目(我还在学习,还是个新手)。我想知道当用户向下滚动侧导航页面内容时,当工具栏与文本重叠时,如何在工具栏中设置标题(驱动程序名称和信息)(詹姆斯史密斯 - PLACA:ABC1234)。

我分叉了一个样板旁导航代码与您分享 https://stackblitz.com/edit/angular-closing-side-nav-in-mobile-z35xfw?file=app/sidenav-responsive-example.html

http://img2.mukewang.com/632472fc0001ed2208310746.jpg

这就是我想实现的目标。

http://img4.mukewang.com/632473060001afa808290478.jpg

白衣染霜花
浏览 111回答 1
1回答

摇曳的蔷薇

我做了这个功能,你可以在stackblitz中看到它,基本上,我做了一个对标题的引用(检查HTML中的第39行),检查这个标题是否可见的逻辑是:this.titleIsVisible =&nbsp; &nbsp; &nbsp; bounding.top >= 0 &&&nbsp; &nbsp; &nbsp; bounding.left >= 0 &&&nbsp; &nbsp; &nbsp; bounding.right <=&nbsp; &nbsp; &nbsp; &nbsp; (window.innerWidth || document.documentElement.clientWidth) &&&nbsp; &nbsp; &nbsp; bounding.bottom <=&nbsp; &nbsp; &nbsp; &nbsp; (window.innerHeight || document.documentElement.clientHeight);为了侦听我在 TS 和我刚刚使用的 HTML 中使用的滚动事件(滚动)。要查看此内容,请检查 TS 中的第 28 行和 HTML 中的第 30 行。@HostListener("window:scroll", ["$event"])以防万一,这是代码。import { Component, ViewChild, ElementRef, HostListener } from "@angular/core";import {&nbsp; BreakpointObserver,&nbsp; Breakpoints,&nbsp; BreakpointState} from "@angular/cdk/layout";import { FormControl } from "@angular/forms";import { Observable } from "rxjs";import { map } from "rxjs/operators";/** @title Responsive sidenav */@Component({&nbsp; selector: "sidenav-responsive-example",&nbsp; templateUrl: "sidenav-responsive-example.html",&nbsp; styleUrls: ["sidenav-responsive-example.css"]})export class SidenavResponsiveExample {&nbsp; @ViewChild("drawer") drawer: any;&nbsp; @ViewChild("title") title: ElementRef;&nbsp; public selectedItem: string = "";&nbsp; public isHandset$: Observable<boolean> = this.breakpointObserver&nbsp; &nbsp; .observe(Breakpoints.Handset)&nbsp; &nbsp; .pipe(map((result: BreakpointState) => result.matches));&nbsp; public titleIsVisible: boolean = true;&nbsp; constructor(private breakpointObserver: BreakpointObserver) {}&nbsp; @HostListener("window:scroll", ["$event"])&nbsp; onScroll() {&nbsp; &nbsp; const bounding = this.title.nativeElement.getBoundingClientRect();&nbsp; &nbsp; this.titleIsVisible =&nbsp; &nbsp; &nbsp; bounding.top >= 0 &&&nbsp; &nbsp; &nbsp; bounding.left >= 0 &&&nbsp; &nbsp; &nbsp; bounding.right <=&nbsp; &nbsp; &nbsp; &nbsp; (window.innerWidth || document.documentElement.clientWidth) &&&nbsp; &nbsp; &nbsp; bounding.bottom <=&nbsp; &nbsp; &nbsp; &nbsp; (window.innerHeight || document.documentElement.clientHeight);&nbsp; }&nbsp; closeSideNav() {&nbsp; &nbsp; if (this.drawer._mode == "over") {&nbsp; &nbsp; &nbsp; this.drawer.close();&nbsp; &nbsp; }&nbsp; }}<mat-sidenav-container class="sidenav-container">&nbsp; <mat-sidenav #drawer fixedInViewport="true" [attr.role]="(isHandset$ | async) ? 'dialog' : 'navigation'"&nbsp; &nbsp; [mode]="(isHandset$ | async) ? 'over' : 'side'" [opened]="!(isHandset$ | async)">&nbsp; &nbsp; <mat-toolbar color="primary" style="margin-top:30px;"> Business</mat-toolbar>&nbsp; &nbsp; <mat-nav-list >&nbsp; &nbsp; &nbsp; <mat-list-item&nbsp; (click)="closeSideNav()">&nbsp; &nbsp; &nbsp; &nbsp; <mat-icon matListIcon>home</mat-icon>&nbsp; &nbsp; &nbsp; &nbsp; <a matLine>Home</a>&nbsp; &nbsp; &nbsp; </mat-list-item>&nbsp; &nbsp; &nbsp; <mat-list-item (click)="closeSideNav()">&nbsp; &nbsp; &nbsp; &nbsp; <mat-icon matListIcon>person</mat-icon>&nbsp; &nbsp; &nbsp; &nbsp; <a matLine>Customers</a>&nbsp; &nbsp; &nbsp; </mat-list-item>&nbsp; &nbsp; &nbsp; <mat-list-item (click)="closeSideNav()">&nbsp; &nbsp; &nbsp; &nbsp; <mat-icon matListIcon>group_work</mat-icon>&nbsp; &nbsp; &nbsp; &nbsp; <a matLine>employees</a>&nbsp; &nbsp; &nbsp; </mat-list-item>&nbsp; &nbsp; &nbsp; <mat-list-item (click)="closeSideNav()">&nbsp; &nbsp; &nbsp; &nbsp; <mat-icon matListIcon>business</mat-icon>&nbsp; &nbsp; &nbsp; &nbsp; <a matLine>business</a>&nbsp; &nbsp; &nbsp; </mat-list-item>&nbsp; &nbsp; </mat-nav-list>&nbsp; </mat-sidenav>&nbsp; <mat-sidenav-content (scroll)="onScroll($event)">&nbsp; &nbsp; <mat-toolbar color="primary">&nbsp; &nbsp; &nbsp; <button type="button" aria-label="Toggle sidenav" mat-icon-button (click)="drawer.toggle()" *ngIf="isHandset$ | async">&nbsp; &nbsp; &nbsp; &nbsp; <mat-icon aria-label="Side nav toggle icon">menu</mat-icon>&nbsp; &nbsp; &nbsp; </button>&nbsp; &nbsp; &nbsp; <h1 *ngIf=!titleIsVisible>TITLE</h1>&nbsp; &nbsp; </mat-toolbar>&nbsp; &nbsp; <div class="ylb-app-content">&nbsp; &nbsp; &nbsp; <h1 #title >TITLE</h1>&nbsp; &nbsp; &nbsp; <div >&nbsp; &nbsp; &nbsp; &nbsp; "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."&nbsp; &nbsp; &nbsp; &nbsp; "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."&nbsp; &nbsp; &nbsp; &nbsp; "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."&nbsp; &nbsp; &nbsp; &nbsp; "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."&nbsp; &nbsp; &nbsp; &nbsp; "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."&nbsp; &nbsp; &nbsp; &nbsp; "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."&nbsp; &nbsp; &nbsp; &nbsp; "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."&nbsp; &nbsp; &nbsp; </div>&nbsp; &nbsp; &nbsp; <ng-content>&nbsp; &nbsp; &nbsp; </ng-content>&nbsp; &nbsp; </div>&nbsp; </mat-sidenav-content></mat-sidenav-container>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript