Add full page scroll to Home

This commit is contained in:
2023-09-12 15:46:25 +08:00
parent c7d91eba8e
commit 1eadcfd476

View File

@@ -13,8 +13,11 @@ const Home: React.FC = () => {
} = useContext(MainFrameworkContext) } = useContext(MainFrameworkContext)
const fitFullScreenRef = useRef<HTMLDivElement>(null) const fitFullScreenRef = useRef<HTMLDivElement>(null)
const scrollTimeout = useRef<number>()
const [slogan, setSlogan] = useState('') const [slogan, setSlogan] = useState('')
const [sloganType, setSloganType] = useState(true) const [sloganType, setSloganType] = useState(true)
const [currentContent, setCurrentContent] = useState(0)
const typeText = '/* 因为热爱 所以折腾 */' const typeText = '/* 因为热爱 所以折腾 */'
if (sloganType) { if (sloganType) {
@@ -42,14 +45,18 @@ const Home: React.FC = () => {
}) })
}, [setNavbarHidden, setPreventScroll]) }, [setNavbarHidden, setPreventScroll])
const handleScrollToDown = () => { const handleScrollToContent = (index: number) => {
hideScrollbarRef.current?.scrollY(fitFullScreenRef.current?.offsetHeight ?? 0) return () => {
if (!index) {
setNavbarHidden(true)
hideScrollbarRef.current?.scrollY(0)
} else {
hideScrollbarRef.current?.scrollY(
(fitFullScreenRef.current?.offsetHeight ?? 0) * index
)
setNavbarHidden(false) setNavbarHidden(false)
} }
}
const handleScrollToTop = () => {
hideScrollbarRef.current?.scrollY(0)
setNavbarHidden(true)
} }
const handleWheel = (event: React.WheelEvent) => { const handleWheel = (event: React.WheelEvent) => {
@@ -58,18 +65,33 @@ const Home: React.FC = () => {
} }
if (event.deltaY > 0) { if (event.deltaY > 0) {
handleScrollToDown() if (currentContent >= content.length) {
setNavbarHidden(false) return
}
handleScrollToContent(currentContent + 1)()
clearTimeout(scrollTimeout.current ?? 0)
scrollTimeout.current = setTimeout(() => {
setCurrentContent(currentContent + 1)
console.log(`up ${currentContent + 1}`)
}, 500)
} else { } else {
handleScrollToTop() if (currentContent <= 0) {
setNavbarHidden(true) return
}
handleScrollToContent(currentContent - 1)()
clearTimeout(scrollTimeout.current ?? 0)
scrollTimeout.current = setTimeout(() => {
setCurrentContent(currentContent - 1)
console.log(`down ${currentContent - 1}`)
}, 500)
} }
} }
return ( const content = [
<> {
<div onWheel={handleWheel}> backgroundColor: '#FBFBFB',
<FitFullScreen backgroundColor={'#FBFBFB'} ref={fitFullScreenRef}> ref: fitFullScreenRef,
children: (
<FitCenter> <FitCenter>
<div className={'center-box'}> <div className={'center-box'}>
<div className={'big-logo'}>FatWeb</div> <div className={'big-logo'}>FatWeb</div>
@@ -77,16 +99,25 @@ const Home: React.FC = () => {
{slogan || <>&nbsp;</>} {slogan || <>&nbsp;</>}
</span> </span>
</div> </div>
<div className={'scroll-down'} onClick={handleScrollToDown}> <div className={'scroll-down'} onClick={handleScrollToContent(1)}>
<Icon <Icon
component={IconFatwebDown} component={IconFatwebDown}
style={{ fontSize: '1.8em', color: '#666' }} style={{ fontSize: '1.8em', color: '#666' }}
/> />
</div> </div>
</FitCenter> </FitCenter>
</FitFullScreen> )
<FitFullScreen /> },
<FitFullScreen /> { children: <FitCenter>2</FitCenter> },
{ children: <FitCenter>3</FitCenter> }
]
return (
<>
<div onWheel={handleWheel}>
{content.map((element, index) => {
return <FitFullScreen key={index} {...element} />
})}
</div> </div>
</> </>
) )