TopNav监听滚动
This commit is contained in:
tangly1024
2021-12-22 21:55:33 +08:00
parent efd1b0b2c5
commit f59a760245
2 changed files with 27 additions and 2 deletions

View File

@@ -1,6 +1,8 @@
import { useGlobal } from '@/lib/global'
import { faArrowDown } from '@fortawesome/free-solid-svg-icons'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import { useEffect, useState } from 'react'
import throttle from 'lodash.throttle'
import { useCallback, useEffect, useState } from 'react'
import Typed from 'typed.js'
/**
@@ -19,6 +21,29 @@ export default function Header () {
const scrollToCenter = () => {
document.getElementById('wrapper').scrollIntoView({ behavior: 'smooth' })
}
const { theme } = useGlobal()
// 监听滚动自动分页加载
const scrollTrigger = useCallback(throttle(() => {
if (theme !== 'dark') {
const stickyNavElement = document.getElementById('sticky-nav')
if (window.scrollY < window.innerHeight) {
stickyNavElement.classList.add('dark')
} else {
stickyNavElement.classList.remove('dark')
}
}
}, 500))
// 监听滚动
useEffect(() => {
scrollTrigger()
window.addEventListener('scroll', scrollTrigger)
return () => {
window.removeEventListener('scroll', scrollTrigger)
}
})
return <div className='h-screen w-full'>
<div className='absolute z-10 flex h-screen items-center justify-center w-full'>
<div id="typed-strings">

View File

@@ -14,7 +14,7 @@ const TopNav = ({ tags, currentTag, post, posts, categories, currentCategory })
<SideBarDrawer post={post} currentTag={currentTag} cRef={drawer} tags={tags} posts={posts} categories={categories} currentCategory={currentCategory}/>
{/* 导航栏 */}
<div id='sticky-nav' className='dark flex fixed md:relative w-full top-0 z-20 transform duration-500'>
<div id='sticky-nav' className='flex fixed md:relative w-full top-0 z-20 transform duration-500'>
<div className='w-full flex justify-between items-center p-4 glassmorphism'>
{/* 左侧LOGO 标题 */}
<div className='flex flex-none flex-grow-0'>