Docusaurus v2 nav link 主页和文档页面上的操作
Docusaurus v2 nav link actions on home page and doc pages
我的 docusaurus 主页上有多个部分,从顶部导航栏我可以通过检查 window.location.pathname==='/'
.
使用下面的代码 scrollTo
预期的部分
let navLink = document.querySelectorAll('nav .navbar__link');
if(navLink) {
navLink.forEach(function(link) {
link.addEventListener('click', function(e){
e.preventDefault();
let selectedSection = e.currentTarget.textContent.toLowerCase();
if(window.location.pathname === '/') {
window.scrollTo(0,document.querySelector(`#anchor-${selectedSection}-section`).offsetTop - 100);
}
})
})
}
当我在主页上时,它工作正常。
通过单击 Home
导航 link 从文档页面返回(路由)主页后,scrollTo
方法不起作用,我直接路由到文档路径而不是滚动到其中一个主页部分。虽然我正在检查 window.location.pathname === '/'
代码没有执行。
谁能帮我弄清楚从文档页面单击时路由幕后发生了什么。
注意:我已经在 index.js
文件上写了上面的脚本。
第 1 步:创建了一个名为 home.js
的新页面
第 2 步:将 docusaurus.config.js link 对象从 /
更新为 /home
{ to: '/home', label: "Home", position: 'left'}
第 3 步:在 home.js
中添加了重定向功能以重定向到“/”,这解决了我的实际问题。
我的 docusaurus 主页上有多个部分,从顶部导航栏我可以通过检查 window.location.pathname==='/'
.
scrollTo
预期的部分
let navLink = document.querySelectorAll('nav .navbar__link');
if(navLink) {
navLink.forEach(function(link) {
link.addEventListener('click', function(e){
e.preventDefault();
let selectedSection = e.currentTarget.textContent.toLowerCase();
if(window.location.pathname === '/') {
window.scrollTo(0,document.querySelector(`#anchor-${selectedSection}-section`).offsetTop - 100);
}
})
})
}
当我在主页上时,它工作正常。
通过单击 Home
导航 link 从文档页面返回(路由)主页后,scrollTo
方法不起作用,我直接路由到文档路径而不是滚动到其中一个主页部分。虽然我正在检查 window.location.pathname === '/'
代码没有执行。
谁能帮我弄清楚从文档页面单击时路由幕后发生了什么。
注意:我已经在 index.js
文件上写了上面的脚本。
第 1 步:创建了一个名为 home.js
的新页面第 2 步:将 docusaurus.config.js link 对象从 /
更新为 /home
{ to: '/home', label: "Home", position: 'left'}
第 3 步:在 home.js
中添加了重定向功能以重定向到“/”,这解决了我的实际问题。