【问题标题】:Why does the whole page push itself up a few pixels when I am tabbing through the page?当我在页面中切换时,为什么整个页面会向上推几个像素?
【发布时间】:2019-12-26 13:55:58
【问题描述】:

我正在网页上实现一项功能,该功能允许用户使用 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);
}

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

其他信息:

  • Angular 8 (8.0.2)
  • 在 3 个浏览器(Edge、IE11、Chrome)中对此进行了测试。该错误出现在所有 3 个中。

编辑

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

谢谢

【问题讨论】:

  • 导航栏是否在滚动时隐藏/显示?我已经看到了一些不好的实现,其中导航栏在滚动时从页面流中取出,这导致页面按导航栏的高度向上跳跃。

标签: html css angular keyboard accessibility


【解决方案1】:

你在问什么?

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

【讨论】:

  • 在尝试了这个之后,我意识到被推上去的不仅仅是菜单,而是整个页面。我已经更新了问题。
猜你喜欢
  • 2022-01-10
  • 2011-06-25
  • 2010-10-30
  • 2022-01-12
  • 1970-01-01
  • 2014-09-29
  • 1970-01-01
  • 2015-02-25
  • 1970-01-01
相关资源
最近更新 更多