From 62584652039e15f7f8e7a5a4c7b6e98fe962d699 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Tue, 26 Oct 2021 16:16:46 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9B=AE=E5=BD=95zIndex=E8=B0=83=E6=95=B4?= =?UTF-8?q?=EF=BC=8C=E5=B0=81=E8=A3=85=E5=AF=BC=E8=88=AA=E9=9A=90=E8=97=8F?= =?UTF-8?q?=E5=87=BD=E6=95=B0=EF=BC=8C=E5=8A=A0=E5=85=A5=E9=9A=90=E7=A7=81?= =?UTF-8?q?=E5=8D=8F=E8=AE=AE=E9=93=BE=E6=8E=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- components/Footer.js | 1 + layouts/BaseLayout.js | 47 ++++++++++++++++++++++++++++++----------- pages/article/[slug].js | 2 +- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/components/Footer.js b/components/Footer.js index cf2e1d83..f6ef5442 100644 --- a/components/Footer.js +++ b/components/Footer.js @@ -10,6 +10,7 @@ const Footer = ({ fullWidth = true }) => { {` ${y}`} tangly1024.com. Powered by Notion & Vercel.
闽ICP备20010331号 + 隐私政策 { const scrollS = window.scrollY - const nav = document.querySelector('#sticky-nav') - const sidebar = document.querySelector('#sidebar') - const tagsBar = document.querySelector('#tags-bar') - const rightToc = document.querySelector('#right-toc') if (scrollS >= windowTop && scrollS > 10) { - nav && nav.classList.replace('top-0', '-top-16') - tagsBar && tagsBar.classList.replace('top-16', 'top-0') - sidebar && sidebar.classList.replace('top-20', 'top-2') - rightToc && rightToc.classList.replace('top-16', 'top-0') + hiddenNav() windowTop = scrollS } else { - nav && nav.classList.replace('-top-16', 'top-0') - tagsBar && tagsBar.classList.replace('top-0', 'top-16') - sidebar && sidebar.classList.replace('top-2', 'top-20') - rightToc && rightToc.classList.replace('top-0', 'top-16') + showNav() windowTop = scrollS } }, 200)) @@ -35,6 +25,7 @@ const BaseLayout = ({ children, layout, fullWidth, tags, meta, post, ...customMe // 监听滚动 useEffect(() => { window.addEventListener('scroll', scrollTrigger) + scrollTrigger() return () => { window.removeEventListener('scroll', scrollTrigger) } @@ -62,6 +53,38 @@ const BaseLayout = ({ children, layout, fullWidth, tags, meta, post, ...customMe ) } +/** + * 隐藏导航 + */ +const hiddenNav = () => { + if (document) { + const nav = document.querySelector('#sticky-nav') + const sidebar = document.querySelector('#sidebar') + const tagsBar = document.querySelector('#tags-bar') + const rightToc = document.querySelector('#right-toc') + nav && nav.classList.replace('top-0', '-top-16') + tagsBar && tagsBar.classList.replace('top-16', 'top-0') + sidebar && sidebar.classList.replace('top-20', 'top-2') + rightToc && rightToc.classList.replace('top-16', 'top-0') + } +} + +/** + * 显示导航 + */ +const showNav = () => { + if (document) { + const nav = document.querySelector('#sticky-nav') + const sidebar = document.querySelector('#sidebar') + const tagsBar = document.querySelector('#tags-bar') + const rightToc = document.querySelector('#right-toc') + nav && nav.classList.replace('-top-16', 'top-0') + tagsBar && tagsBar.classList.replace('top-0', 'top-16') + sidebar && sidebar.classList.replace('top-2', 'top-20') + rightToc && rightToc.classList.replace('top-0', 'top-16') + } +} + BaseLayout.propTypes = { children: PropTypes.node } diff --git a/pages/article/[slug].js b/pages/article/[slug].js index 392c6ca8..c9edda52 100644 --- a/pages/article/[slug].js +++ b/pages/article/[slug].js @@ -141,7 +141,7 @@ const BlogPost = ({ post, blockMap, tags, prev, next }) => { {/* 右侧目录 */} -