简体   繁体   English

固定 header 阻塞锚链接

[英]fixed header blocking anchor links

i have a fixed navigation menu header that is “sticky” as you scroll, chances are you've run into this issue.我有一个固定的导航菜单 header,当您滚动时它是“粘性的”,您可能遇到过这个问题。 When you click on a link to an anchor further down the page, it scrolls so the anchor is now at the very top of your window…behind the nav header that, of course, has remained at the top of the screen.当您单击指向页面下方锚点的链接时,它会滚动,因此锚点现在位于窗口的最顶部……在导航 header 的后面,当然,它一直保留在屏幕的顶部。

i tried to use anchor class but was not able to get it to work我尝试使用锚点 class 但无法正常工作

 
 ::selection {
  background: #51565D;
}
.content {
  padding: 16px;
}
 
 html {
scroll-behavior: smooth;
}
 
.anchor {
display: block;
margin-top: 7%;
visibility: hidden;
}
  
#projects, #products, #contact, #volunteer {
 font-family: "Lucida Console", "Courier New", monospace;
 font-weight: bold;
 text-align: center;
 }

.sticky {
  position: fixed;
  top: 0;
  width: 100%;
}

.sticky + .content {
  padding-top: 60px;
}

.img {
  margin-top: 8px;
}
.container{
  display: flex;
  list-style-type: none;
  justify-content: center;
  flex-wrap: wrap;
}
</style>

I recommend adding a padding to the top of the anchor element, that is the same size as the height of the sticky element (pushing the contents into view)我建议在锚元素的顶部添加一个填充,它的大小与粘性元素的高度相同(将内容推入视图)

For example (and just a guess) maybe you could change例如(只是一个猜测)也许你可以改变

margin-top: 7%;保证金顶部:7%; to padding-top: 7%填充顶部:7%

Found it:找到了:

.anchor:target {
  padding-top: 60px;
  scroll-margin-top: 60px;
}

#projects,
#products,
#contact,
#volunteer {
  font-family: "Lucida Console", "Courier New", monospace;
  font-weight: bold;
  text-align: center;
  scroll-margin-top: 60px;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM