具有固定标题的页面导航
我的网站顶部有一个水平固定标题,当我使用页面导航滚动到 ID 时,它会将我尝试滚动到标题下方的内容放在其中。
有没有办法可以将页面导航显示的位置偏移 80 像素(向下)?
编辑:我实际上能够以最简单和最可接受的方式自己修复它。我将解决方案放在下面的答案中。
I have a horizontal fixed header on top of my site and when I use page navigation to scroll to an ID, it puts the content I'm trying to scroll to underneath the header.
Is there a way I can offset where the page navigation shows up by 80 pixels (down)?
Edit: I was actually able to fix it myself in the simplest and most acceptable manner possible. I put the solution in an answer below.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
好吧,既然没有人有答案,我自己解决了。
这是修复方法:
JSFiddle
这是通过制作一个绝对隐藏的
div
来完成的在我想要滚动到的内容上方放置“x”个像素量。然后,我滚动到该div
而不是我最初想要滚动到的内容。 “x”应该是标题的高度,这样您想要滚动到的内容就会像它应该的那样出现在标题的正下方。Well, since no one else had an answer, I fixed it myself.
Here is the fix:
JSFiddle
This was done by making a hidden
div
that is absolutely positoned 'x' amount of pixlels above the content I want to scroll to. I then scroll to thatdiv
instead of the content I originally wanted to scroll to. 'x' should be the height of your header, this way the content you want to scroll to appears directly below your header like it should.您可以使用 CSS 来做到这一点。
HTML:
CSS:
You can do that with CSS.
HTML:
CSS:
尝试阅读 Chris Coyier 的这篇文章。他巧妙地使用伪元素来解决“页面导航中的固定标题”问题。看一下: http://css-tricks.com/hash-tag-links-填充/。
Try reading this artcle from Chris Coyier. He cleverly uses a pseudo-element to solve the "fixed header in page navigation" issue. Take a look: http://css-tricks.com/hash-tag-links-padding/.
该示例没有显示它是如何工作的。我通过添加修复了它:
The example doesn't show how it works. I fixed it by adding: