html - 固定页眉与页内 anchor 重叠

标签 html url anchor

如果我在 HTML 页面中有一个非滚动标题,固定在顶部,具有定义的高度:

有没有办法使用 URL anchor (#fragment 部分)让浏览器滚动到页面中的某个点,但仍然尊重固定元素的高度 没有 JavaScript 的帮助

http://example.com/#bar
WRONG (but the common behavior):         CORRECT:
+---------------------------------+      +---------------------------------+
| BAR///////////////////// header |      | //////////////////////// header |
+---------------------------------+      +---------------------------------+
| Here is the rest of the Text    |      | BAR                             |
| ...                             |      |                                 |
| ...                             |      | Here is the rest of the Text    |
| ...                             |      | ...                             |
+---------------------------------+      +---------------------------------+

最佳答案

如果您不能或不想设置新类,请添加一个固定高度的 ::before :target 的伪元素CSS 中的伪类:

:target::before {
  content: "";
  display: block;
  height: 60px; /* fixed header height*/
  margin: -60px 0 0; /* negative fixed header height */
}

或者使用 jQuery 滚动相对于 :target 的页面:

var offset = $(':target').offset();
var scrollto = offset.top - 60; // minus fixed header height
$('html, body').animate({scrollTop:scrollto}, 0);

关于html - 固定页眉与页内 anchor 重叠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39458433/

相关文章:

php - 显示具有特定登录 ID 的数据库中的数据

javascript - 如何去除聊天框的阴影区域

ios - 无法使用 Kingfisher 显示 url 中的图像

html - CSS/Bootstrap 3 : remove styling from links

php - 奇怪的 anchor 标记行为

javascript - 如何使用 JQuery/JavaScript/PHP 将文本写入外部文件?

javascript - 如何渲染任意深度的嵌套列表?

java - 如何用Java以编程方式访问网站?

java - 测试 url 连接 java html

html - 我怎样才能在这些列表项中找到一个 anchor 来填充列表项的空间?