From 1eadcfd4765785a7b22813ddbcf85fff90d08271 Mon Sep 17 00:00:00 2001 From: FatttSnake Date: Tue, 12 Sep 2023 15:46:25 +0800 Subject: [PATCH] Add full page scroll to Home --- src/components/Home.tsx | 91 +++++++++++++++++++++++++++-------------- 1 file changed, 61 insertions(+), 30 deletions(-) diff --git a/src/components/Home.tsx b/src/components/Home.tsx index 9a47331..51af362 100644 --- a/src/components/Home.tsx +++ b/src/components/Home.tsx @@ -13,8 +13,11 @@ const Home: React.FC = () => { } = useContext(MainFrameworkContext) const fitFullScreenRef = useRef(null) + const scrollTimeout = useRef() + const [slogan, setSlogan] = useState('') const [sloganType, setSloganType] = useState(true) + const [currentContent, setCurrentContent] = useState(0) const typeText = '/* 因为热爱 所以折腾 */' if (sloganType) { @@ -42,14 +45,18 @@ const Home: React.FC = () => { }) }, [setNavbarHidden, setPreventScroll]) - const handleScrollToDown = () => { - hideScrollbarRef.current?.scrollY(fitFullScreenRef.current?.offsetHeight ?? 0) - setNavbarHidden(false) - } - - const handleScrollToTop = () => { - hideScrollbarRef.current?.scrollY(0) - setNavbarHidden(true) + const handleScrollToContent = (index: number) => { + return () => { + if (!index) { + setNavbarHidden(true) + hideScrollbarRef.current?.scrollY(0) + } else { + hideScrollbarRef.current?.scrollY( + (fitFullScreenRef.current?.offsetHeight ?? 0) * index + ) + setNavbarHidden(false) + } + } } const handleWheel = (event: React.WheelEvent) => { @@ -58,35 +65,59 @@ const Home: React.FC = () => { } if (event.deltaY > 0) { - handleScrollToDown() - setNavbarHidden(false) + if (currentContent >= content.length) { + return + } + handleScrollToContent(currentContent + 1)() + clearTimeout(scrollTimeout.current ?? 0) + scrollTimeout.current = setTimeout(() => { + setCurrentContent(currentContent + 1) + console.log(`up ${currentContent + 1}`) + }, 500) } else { - handleScrollToTop() - setNavbarHidden(true) + if (currentContent <= 0) { + return + } + handleScrollToContent(currentContent - 1)() + clearTimeout(scrollTimeout.current ?? 0) + scrollTimeout.current = setTimeout(() => { + setCurrentContent(currentContent - 1) + console.log(`down ${currentContent - 1}`) + }, 500) } } + const content = [ + { + backgroundColor: '#FBFBFB', + ref: fitFullScreenRef, + children: ( + +
+
FatWeb
+ + {slogan || <> } + +
+
+ +
+
+ ) + }, + { children: 2 }, + { children: 3 } + ] + return ( <>
- - -
-
FatWeb
- - {slogan || <> } - -
-
- -
-
-
- - + {content.map((element, index) => { + return + })}
)