feature: 滚动调整

This commit is contained in:
tangly1024
2021-12-24 14:02:41 +08:00
parent 87320f4689
commit a1a321bd01

View File

@@ -2,8 +2,7 @@ import BLOG from '@/blog.config'
import { useGlobal } from '@/lib/global' import { useGlobal } from '@/lib/global'
import { faAngleDown } from '@fortawesome/free-solid-svg-icons' import { faAngleDown } from '@fortawesome/free-solid-svg-icons'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
import throttle from 'lodash.throttle' import { useEffect, useState } from 'react'
import { useCallback, useEffect, useState } from 'react'
import Typed from 'typed.js' import Typed from 'typed.js'
/** /**
@@ -32,32 +31,38 @@ export default function Header () {
let autoScroll = false let autoScroll = false
const autoScrollEnd = () => { const autoScrollEnd = () => {
windowTop = window.scrollY if (autoScroll) {
autoScroll = false console.log('结束自动滚动')
windowTop = window.scrollY
autoScroll = false
}
} }
const scrollTrigger = throttle(() => {
console.log('触发 scrollTrigger')
const scrollTrigger = () => {
if ( if (
(window.scrollY > windowTop) & (window.scrollY > windowTop) &
(window.scrollY < window.innerHeight) & (window.scrollY < window.innerHeight) &
!autoScroll !autoScroll
) { ) {
console.log('自动滚下', window.scrollY, windowTop)
autoScroll = true autoScroll = true
scrollTo(wrapperTop, autoScrollEnd) window.scrollTo({ top: wrapperTop, behavior: 'smooth' })
setTimeout(autoScrollEnd, 500)
} }
if ( if (
(window.scrollY < windowTop) & (window.scrollY < windowTop) &
(window.scrollY < window.innerHeight) & (window.scrollY < window.innerHeight) &
!autoScroll !autoScroll
) { ) {
console.log('自动滚上')
autoScroll = true autoScroll = true
scrollTo(0, autoScrollEnd) window.scrollTo({ top: 0, behavior: 'smooth' })
setTimeout(autoScrollEnd, 500)
} }
windowTop = window.scrollY windowTop = window.scrollY
updateTopNav() updateTopNav()
}, 500) }
const updateTopNav = () => { const updateTopNav = () => {
if (theme !== 'dark') { if (theme !== 'dark') {
@@ -92,19 +97,19 @@ export default function Header () {
return ( return (
<header <header
id="header" id="header"
className="duration-500 w-full bg-cover bg-center md:-mt-14 bg-black" className="duration-500 w-full bg-cover bg-center lg:-mt-14 bg-black"
style={{ style={{
height: 'calc(100vh + 1px)', height: 'calc(100vh + 1px)',
backgroundImage: backgroundImage:
`linear-gradient(rgba(0, 0, 0, 0.8), rgba(0,0,0,0.2), rgba(0, 0, 0, 0.8) ),url("${BLOG.bannerImage}")` `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 className="absolute flex h-full items-center lg:-mt-14 justify-center w-full text-4xl md:text-7xl text-white">
<div id='typed' className='flex text-center font-serif'/> <div id='typed' className='flex text-center font-serif'/>
</div> </div>
<div <div
onClick={() => { onClick={() => {
scrollTo(wrapperTop, autoScrollEnd) window.scrollTo({ top: wrapperTop, behavior: 'smooth' })
}} }}
className="cursor-pointer w-full text-center py-4 text-5xl animate-bounce absolute bottom-10 text-white" className="cursor-pointer w-full text-center py-4 text-5xl animate-bounce absolute bottom-10 text-white"
> >
@@ -113,27 +118,3 @@ export default function Header () {
</header> </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)