Angular Material 嵌入应用页脚出现双滚动条问题求助
双滚动条问题解决方案
问题根因
双滚动条的来源是两个独立的滚动视口同时存在:
- 你给
.main-content手动配置了height: 100%和overflow: auto,让它变成了一个独立的滚动区域,路由页面的内容在这个区域内部滚动 - 页脚通过内容投影,被放进了Angular Material自动生成的
.mat-sidenav-content容器里,这个容器本身占满了侧边栏容器除侧边栏外的全部可视高度 - 100%高度的主内容块,加上固定高度的页脚,总高度超出了
.mat-sidenav-content的可视范围,触发了外层容器的自动滚动,最终就出现了两个平行的滚动条,和你截图里的表现完全一致。
具体修改方案
1. 重置主内容容器的独立滚动配置
删掉.main-content上导致独立滚动的固定高度、溢出滚动配置,改成由弹性布局分配高度:
.main-content { // 移除原有配置:height: 100%; overflow: auto; padding: { top: 0; left: 15px; right: 15px; bottom: 0; } @include breakpoint($narrow-devices) { padding: { left: 15px; right: 15px; } } // 新增弹性布局配置:占满容器剩余空间 flex: 1; overflow: hidden; & ::ng-deep .outlet, & ::ng-deep .maxed-width { @include breakpoint($narrow-devices) { max-width: $page-max-width; margin: { left: auto; right: auto; } } } }
2. 统一滚动视口到Material侧边栏的内容容器上
让.mat-sidenav-container和它内部生成的内容容器用弹性流排布,把所有滚动逻辑集中在一个容器上:
:host ::ng-deep .mat-sidenav-container[fullscreen] { top: 55px; display: flex; flex-direction: column; @include breakpoint($narrow-devices) { top: 64px; } } :host ::ng-deep .mat-sidenav-content { transform: none !important; // 集中处理滚动逻辑 flex: 1; height: auto; overflow: auto; display: flex; flex-direction: column; } .sidenav-container { flex: 1; }
3. 修正页脚的布局属性
让页脚在弹性流里固定排在最后,不会被压缩变形:
.app-footer-main { background-color: black; flex-shrink: 0; width: 100%; } .app-footer-items { position: relative; display: flex; font-size: small; justify-content: center; }
调整后的效果
- 整个应用只会保留一个统一的滚动视口,彻底消除双滚动条问题
- 当路由页面内容很短时,主内容块会占满所有剩余空间,页脚自动贴在整个可视区域的最底部
- 当路由页面内容很长时,页脚会自然排在所有页面内容的末尾,向下滚动到内容最底端就能看到页脚,不会出现页脚悬浮遮挡、内容被裁切的问题
注意:调整过程中一定要删掉
.main-content上原来的height:100%配置,否则会把主内容块固定为满屏高度,把页脚挤出可视范围,反而重新触发外层滚动。
内容的提问来源于stack exchange,提问作者Ali Celebi
相关产品推荐
相关产品推荐

