简体   繁体   English

Gatsby 平滑滚动锚链接在部署到 github 页面时不起作用

[英]Gatsby Smooth Scroll Anchor Links doesnt work when deployed to github pages

I have a Gatsby website which consist of a few pages.我有一个由几页组成的 Gatsby 网站。 On the navbar, there are different links.在导航栏上,有不同的链接。 Some links point to different pages while some links will bring you to that particular section on that page with a smooth scrolling effect.一些链接指向不同的页面,而一些链接会将您带到该页面上的特定部分,并具有平滑的滚动效果。

TO achieve this, I used Gatsby smooth scroll anchor links.为此,我使用了 Gatsby 平滑滚动锚链接。 Documentation is here: https://www.gatsbyjs.org/packages/gatsby-plugin-anchor-links/ .文档在这里: https://www.gatsbyjs.org/packages/gatsby-plugin-anchor-links/

This is basically what Im trying to achieve:这基本上是我试图实现的目标:

Many sites use a mixed navigation format in which some links route to other pages, while some anchor a smooth scroll to sections within a specific page — but both types still need to function well regardless of what page the user is currently on.许多网站使用混合导航格式,其中一些链接路由到其他页面,而一些链接将平滑滚动锚定到特定页面中的部分 - 但无论用户当前在哪个页面上,这两种类型仍然需要 function 很好。

Here's a snippet of my navbar code:这是我的导航栏代码片段:

             <MDBNavItem className="mr-4">
                <AnchorLink to="/#buy-now" 
                 title="Buy AIEOU">
                  <span>Buy AIEOU</span>
                </AnchorLink>
              </MDBNavItem>
              <MDBNavItem className="mr-4">
                <AnchorLink to="/about" title="Our team">
                  <span>About Us</span>
                </AnchorLink>
              </MDBNavItem>

What's odd is that it totally works fine when I use it locally.奇怪的是,当我在本地使用它时,它完全可以正常工作。 But it doesnt work anymore when I deploy it to Github pages.但是当我将它部署到 Github 页面时,它不再起作用了。 Instead of the smooth scrolling, it just teleports to that section instead.它不是平滑滚动,而是直接传送到该部分。 I added the offset options and also the StripHash attribute but it still doesn;t work.我添加了偏移选项和 StripHash 属性,但它仍然不起作用;不起作用。 Any tips would be appreciated.任何提示将不胜感激。

```

Assuming that you have everything well configured as it seems and that you say that it teleports to the sections (the behavior is correct, the effect not) I guess you need to add the following CSS rule to add smooth behavior:假设您已经按照看起来的方式正确配置了所有内容,并且您说它传送到各个部分(行为正确,效果不正确)我想您需要添加以下 CSS 规则以添加平滑行为:

html,
body {
  scroll-behavior: smooth;
}

The fallback for the browsers at Can I use . Can I use中浏览器的后备方案。

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

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