html - 当我在页面中切换时,为什么整个页面会向上推几个像素?
问题描述
我正在网页上实现一项功能,该功能允许用户使用 tab 键进行导航。
当我浏览页面时,最终页面将不得不自动向下滚动到标签到新内容。发生这种情况时,整个页面会向上移动几个像素,这可以通过导航栏从页面顶部消失来证明。(停留在 DOM 中,只是看不见)。
演示:
代码:
app.component.html:
<div *ngIf="token" class="route-container">
<app-search [token]="token"></app-search>
<router-outlet></router-outlet>
</div>
app.component.scss:
.route-container {
height: 100%;
flex-direction: column;
justify-content: space-between;
}
app-search{
position: relative;
z-index: 1;
}
search.component.html:
<nav class="navHeader">
...
</nav>
search.component.scss:
.navHeader {
height: 48px;
display: flex;
align-items: center;
color: #808080;
box-shadow: 0 1px 3px 0 rgba(0, 0, 0, 0.2), 0 2px 1px -1px rgba(0, 0, 0, 0.12), 0 1px 1px 0 rgba(0, 0, 0, 0.14);
}
为什么会发生这种情况,我该如何解决?
其他信息:
- 角 8 (8.0.2)
- 在 3 个浏览器(Edge、IE11、Chrome)中对此进行了测试。该错误出现在所有 3 个中。
编辑
在尝试了@Eliseo 的解决方案后,我将问题的标题从“为什么导航移动”更改为“为什么整个页面会自行推动”,因为我意识到这不仅仅是菜单被推高了几个像素。
谢谢
解决方案
你在问什么?
.navHeader {
position: fixed; /* Set the navbar to fixed position */
top: 0; /* Position the navbar at the top of the page */
width: 100%; /* Full width */
}
推荐阅读
- emptydatatext - 清空多文件记事本++
- python - 石头剪刀布总是让我赢
- python - 大规模的 Numpy 中值精度问题
- django - Django cors 没有启用?
- sql - 是否有一种有效的方法/函数来处理 SQL 中的等式中的空值组合?
- javascript - 使用 sinon 如何避免测试嵌套函数?
- nginx - 如果重新加载nginx时出错,nginx如何维护现有版本?
- c++ - 如何正确粘贴来自 Arduino IDE 中#include 的代码?
- r-markdown - 将两列插入 R Markdown 以进行 Word 输出
- javascript - 如何使用laravel和vue js在单表数据中水平显示数据