From b10f1ffbfc412dfe77a4c1f3bdc3fc4cab8290e7 Mon Sep 17 00:00:00 2001 From: tangly1024 Date: Sat, 10 Jun 2023 18:51:34 +0800 Subject: [PATCH] bg-header --- themes/hexo/components/Header.js | 72 ++++++++---------------------- themes/matery/components/Header.js | 59 ++++-------------------- themes/matery/config_matery.js | 1 + 3 files changed, 28 insertions(+), 104 deletions(-) diff --git a/themes/hexo/components/Header.js b/themes/hexo/components/Header.js index e72a931a..88fca1c2 100644 --- a/themes/hexo/components/Header.js +++ b/themes/hexo/components/Header.js @@ -1,16 +1,12 @@ // import Image from 'next/image' -import { useCallback, useEffect, useState } from 'react' +import { useEffect, useState } from 'react' import Typed from 'typed.js' import CONFIG_HEXO from '../config_hexo' import NavButtonGroup from './NavButtonGroup' -import throttle from 'lodash.throttle' import { useGlobal } from '@/lib/global' import BLOG from '@/blog.config' let wrapperTop = 0 -let windowTop = 0 -let autoScroll = true -const enableAutoScroll = false // 是否开启自动吸附滚动 /** * @@ -20,6 +16,9 @@ const Header = props => { const [typed, changeType] = useState() const { siteInfo } = props const { locale } = useGlobal() + const scrollToWrapper = () => { + window.scrollTo({ top: wrapperTop, behavior: 'smooth' }) + } useEffect(() => { updateHeaderHeight() @@ -36,16 +35,8 @@ const Header = props => { ) } - if (enableAutoScroll) { - scrollTrigger() - window.addEventListener('scroll', scrollTrigger) - } - window.addEventListener('resize', updateHeaderHeight) return () => { - if (enableAutoScroll) { - window.removeEventListener('scroll', scrollTrigger) - } window.removeEventListener('resize', updateHeaderHeight) } }) @@ -57,58 +48,33 @@ const Header = props => { }) } - const autoScrollEnd = () => { - if (autoScroll) { - windowTop = window.scrollY - autoScroll = false - } - } - const throttleMs = 200 - const scrollTrigger = useCallback(throttle(() => { - if (screen.width <= 768) { - return - } - - const scrollS = window.scrollY - // 自动滚动 - if ((scrollS > windowTop) & (scrollS < window.innerHeight) && !autoScroll - ) { - autoScroll = true - window.scrollTo({ top: wrapperTop, behavior: 'smooth' }) - autoScrollEnd() - } - if ((scrollS < windowTop) && (scrollS < window.innerHeight) && !autoScroll) { - autoScroll = true - window.scrollTo({ top: 0, behavior: 'smooth' }) - autoScrollEnd() - } - windowTop = scrollS - }, throttleMs)) - return ( -