Files
NotionNext/components/Header.js
tangly1024 87320f4689 bugfix:
修复按钮点不到
2021-12-31 15:29:25 +08:00

140 lines
3.6 KiB
JavaScript

import BLOG from '@/blog.config'
import { useGlobal } from '@/lib/global'
import { faAngleDown } from '@fortawesome/free-solid-svg-icons'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import throttle from 'lodash.throttle'
import { useCallback, useEffect, useState } from 'react'
import Typed from 'typed.js'
/**
*
* @returns 头图
*/
export default function Header () {
const [typed, changeType] = useState()
useEffect(() => {
if (!typed && window && document.getElementById('typed')) {
changeType(
new Typed('#typed', {
strings: BLOG.headerStrings,
typeSpeed: 200,
backSpeed: 100,
backDelay: 400,
showCursor: true,
smartBackspace: true
})
)
}
})
const { theme } = useGlobal()
// 监听滚动
let windowTop = 0
let autoScroll = false
const autoScrollEnd = () => {
windowTop = window.scrollY
autoScroll = false
}
const scrollTrigger = throttle(() => {
console.log('触发 scrollTrigger')
if (
(window.scrollY > windowTop) &
(window.scrollY < window.innerHeight) &
!autoScroll
) {
autoScroll = true
scrollTo(wrapperTop, autoScrollEnd)
}
if (
(window.scrollY < windowTop) &
(window.scrollY < window.innerHeight) &
!autoScroll
) {
autoScroll = true
scrollTo(0, autoScrollEnd)
}
windowTop = window.scrollY
updateTopNav()
}, 500)
const updateTopNav = () => {
if (theme !== 'dark') {
const stickyNavElement = document.getElementById('sticky-nav')
if (window.scrollY < window.innerHeight) {
stickyNavElement.classList.add('dark')
} else {
stickyNavElement.classList.remove('dark')
}
}
}
let wrapperTop = 0
function updateHeaderHeight () {
if (window) {
const wrapperElement = document.getElementById('wrapper')
wrapperTop = wrapperElement.offsetTop
}
}
useEffect(() => {
updateHeaderHeight()
updateTopNav()
window.addEventListener('scroll', scrollTrigger)
window.addEventListener('resize', updateHeaderHeight)
return () => {
window.removeEventListener('scroll', scrollTrigger)
window.removeEventListener('resize', updateHeaderHeight)
}
})
return (
<header
id="header"
className="duration-500 w-full bg-cover bg-center md:-mt-14 bg-black"
style={{
height: 'calc(100vh + 1px)',
backgroundImage:
`linear-gradient(rgba(0, 0, 0, 0.8), rgba(0,0,0,0.2), rgba(0, 0, 0, 0.8) ),url("${BLOG.bannerImage}")`
}}
>
<div className="absolute flex h-full items-center -mt-14 justify-center w-full text-4xl md:text-7xl text-white">
<div id='typed' className='flex text-center font-serif'/>
</div>
<div
onClick={() => {
scrollTo(wrapperTop, autoScrollEnd)
}}
className="cursor-pointer w-full text-center py-4 text-5xl animate-bounce absolute bottom-10 text-white"
>
<FontAwesomeIcon icon={faAngleDown} />
</div>
</header>
)
}
/**
* Native scrollTo with callback
* @param offset - offset to scroll to
* @param callback - callback function
*/
const scrollTo = throttle((offset, callback) => {
const fixedOffset = offset.toFixed()
const onScroll = function () {
console.log('触发 scrollTo')
if (window.pageYOffset.toFixed() === fixedOffset) {
window.removeEventListener('scroll', onScroll)
window.onscroll = function () {}
callback()
}
}
window.addEventListener('scroll', onScroll)
window.onscroll = onScroll()
window.scrollTo({
top: offset,
behavior: 'smooth'
})
}, 500)