javascript 滚动到带有固定标题的锚点、隐藏在标题后面的内容、边距和顶部填充不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4835885/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 15:02:25  来源:igfitidea点击:

Scroll to anchor with fixed header, content hidden behind header, margin and top padding not working

javascriptheaderscrollanchorfixed

提问by Commandrea

I'm using smoothscroll.js to navigate my site. It stops on the correct anchor in Firefox, however in Chrome it passes the point on the first click of the link, and pushes the content to the top, hiding the content behind the page. then if you click it again, it aligns correctly. I've seen the problems on other sites as well. Curious to see if others have the same problem. Chrome seems to ignore any top padding or margin or positioning (example: top:20%;).

我正在使用 smoothscroll.js 来浏览我的网站。它在 Firefox 中停在正确的锚点上,但在 Chrome 中它会在第一次点击链接时通过该点,并将内容推到顶部,将内容隐藏在页面后面。然后,如果您再次单击它,它会正确对齐。我在其他网站上也看到了这些问题。想看看其他人是否有同样的问题。Chrome 似乎忽略了任何顶部填充或边距或定位(例如:top:20%;)。

回答by optimiertes

I used Local Scroll (JQuery)to do this.

我使用Local Scroll (JQuery)来做到这一点。

It allows you to do a top offset

它允许您进行顶部偏移

$.localScroll({
    offset: -100
});

回答by Ash Andrien

This may be due to a new wordpress theme using the header html5 element. I changed my header to a div, and it worked fine.

这可能是由于使用标题 html5 元素的新 wordpress 主题。我将标题更改为 div,效果很好。