From 4687c7f08fcab1c9397d2e372fa2fa9904f5416e Mon Sep 17 00:00:00 2001 From: "tangly1024.com" Date: Wed, 1 Feb 2023 14:13:50 +0800 Subject: [PATCH] =?UTF-8?q?hexo=20=E5=85=B3=E9=97=AD=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E6=BB=9A=E5=8A=A8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- themes/hexo/components/Header.js | 75 +++++++++++++++++--------------- 1 file changed, 41 insertions(+), 34 deletions(-) diff --git a/themes/hexo/components/Header.js b/themes/hexo/components/Header.js index bf5e5d09..fa2c5e39 100644 --- a/themes/hexo/components/Header.js +++ b/themes/hexo/components/Header.js @@ -15,7 +15,6 @@ const Header = props => { const [typed, changeType] = useState() const { siteInfo } = props useEffect(() => { - scrollTrigger() updateHeaderHeight() if (!typed && window && document.getElementById('typed')) { changeType( @@ -29,45 +28,20 @@ const Header = props => { }) ) } - window.addEventListener('scroll', scrollTrigger) + if (enableAutoScroll) { + scrollTrigger() + window.addEventListener('scroll', scrollTrigger) + } + window.addEventListener('resize', updateHeaderHeight) return () => { - window.removeEventListener('scroll', scrollTrigger) + if (enableAutoScroll) { + window.removeEventListener('scroll', scrollTrigger) + } window.removeEventListener('resize', updateHeaderHeight) } }) - const autoScrollEnd = () => { - if (autoScroll) { - windowTop = window.scrollY - autoScroll = false - } - } - - /** - * 自动吸附滚动,移动端体验不好暂时关闭 - */ - const scrollTrigger = () => { - if (screen.width <= 768) { - return - } - - const scrollS = window.scrollY - // 自动滚动 - if ((scrollS > windowTop) & (scrollS < window.innerHeight) && !autoScroll - ) { - autoScroll = true - window.scrollTo({ top: wrapperTop, behavior: 'smooth' }) - setTimeout(autoScrollEnd, 500) - } - if ((scrollS < windowTop) && (scrollS < window.innerHeight) && !autoScroll) { - autoScroll = true - window.scrollTo({ top: 0, behavior: 'smooth' }) - setTimeout(autoScrollEnd, 500) - } - windowTop = scrollS - } - function updateHeaderHeight () { setTimeout(() => { const wrapperElement = document.getElementById('wrapper') @@ -106,4 +80,37 @@ const Header = props => { ) } +const enableAutoScroll = false // 是否开启自动吸附滚动 + +const autoScrollEnd = () => { + if (autoScroll) { + windowTop = window.scrollY + autoScroll = false + } +} + +/** + * 自动吸附滚动,移动端体验不好暂时关闭 + */ +const scrollTrigger = () => { + if (screen.width <= 768) { + return + } + + const scrollS = window.scrollY + // 自动滚动 + if ((scrollS > windowTop) & (scrollS < window.innerHeight) && !autoScroll + ) { + autoScroll = true + window.scrollTo({ top: wrapperTop, behavior: 'smooth' }) + setTimeout(autoScrollEnd, 500) + } + if ((scrollS < windowTop) && (scrollS < window.innerHeight) && !autoScroll) { + autoScroll = true + window.scrollTo({ top: 0, behavior: 'smooth' }) + setTimeout(autoScrollEnd, 500) + } + windowTop = scrollS +} + export default Header