diff --git a/components/Header.js b/components/Header.js index bd9776a9..23c0bba2 100644 --- a/components/Header.js +++ b/components/Header.js @@ -12,9 +12,14 @@ import Typed from 'typed.js' export default function Header () { const [typed, changeType] = useState() useEffect(() => { - if (!typed) { + if (!typed && window && document.getElementById('typed')) { changeType(new Typed('#typed', { - stringsElement: '#typed-strings', typeSpeed: 200, showCursor: false + strings: ['Hi,我是一个程序员', 'Hi,我是一个打工人', 'Hi,我是一个干饭人', '欢迎来到我的博客🎉'], + typeSpeed: 200, + backSpeed: 100, + backDelay: 400, + showCursor: true, + smartBackspace: true })) } }) @@ -23,8 +28,20 @@ export default function Header () { } const { theme } = useGlobal() - // 监听滚动自动分页加载 + // 监听滚动 + let windowTop = 0 const scrollTrigger = useCallback(throttle(() => { + if (window.scrollY > windowTop & window.scrollY < window.innerHeight) { + scrollToCenter() + } + if (window.scrollY < windowTop & window.scrollY < window.innerHeight) { + window.scroll({ top: 0, behavior: 'smooth' }) + } + windowTop = window.scrollY + updateTopNav() + }, 500)) + + const updateTopNav = () => { if (theme !== 'dark') { const stickyNavElement = document.getElementById('sticky-nav') if (window.scrollY < window.innerHeight) { @@ -33,27 +50,26 @@ export default function Header () { stickyNavElement.classList.remove('dark') } } - }, 500)) + } // 监听滚动 useEffect(() => { - scrollTrigger() + updateTopNav() window.addEventListener('scroll', scrollTrigger) return () => { window.removeEventListener('scroll', scrollTrigger) } }) - return