首页 > 解决方案 > 当我在页面中切换时,为什么整个页面会向上推几个像素?

问题描述

我正在网页上实现一项功能,该功能允许用户使用 tab 键进行导航。

当我浏览页面时,最终页面将不得不自动向下滚动到标签到新内容。发生这种情况时,整个页面会向上移动几个像素,这可以通过导航栏从页面顶部消失来证明。(停留在 DOM 中,只是看不见)。

演示:

演示:Tab 时导航栏消失

代码:

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);
}

为什么会发生这种情况,我该如何解决?

其他信息:

编辑

在尝试了@Eliseo 的解决方案后,我将问题的标题从“为什么导航移动”更改为“为什么整个页面会自行推动”,因为我意识到这不仅仅是菜单被推高了几个像素。

谢谢

标签: htmlcssangularkeyboardaccessibility

解决方案


你在问什么?

.navHeader {
  position: fixed; /* Set the navbar to fixed position */
  top: 0; /* Position the navbar at the top of the page */
  width: 100%; /* Full width */
}

推荐阅读