diff --git a/src/components/BackToTop/index.tsx b/src/components/BackToTop/index.tsx new file mode 100644 index 0000000000..9a97012034 --- /dev/null +++ b/src/components/BackToTop/index.tsx @@ -0,0 +1,65 @@ +import React, { useEffect, useState } from "react" + +interface Props { + scrollThreshold: number +} + +const BackToTop: React.FC = ({ scrollThreshold }) => { + const [isVisible, setIsVisible] = useState(false) + + useEffect(() => { + const handleScroll = () => { + const pageHeight = document.body.scrollHeight + const currentPosition = window.pageYOffset + console.log(pageHeight, currentPosition) + if (pageHeight < 6000) { + setIsVisible(false) + } else { + if (currentPosition > scrollThreshold) { + setIsVisible(true) + } else { + setIsVisible(false) + } + } + } + window.addEventListener("scroll", handleScroll) + return () => { + window.removeEventListener("scroll", handleScroll) + } + }, [scrollThreshold]) + + const handleBackToTopClick = () => { + window.scrollTo({ + top: 0, + behavior: "smooth", + }) + } + + return ( + <> + {isVisible && ( + + )} + + ) +} + +export default BackToTop diff --git a/src/components/Layout/index.tsx b/src/components/Layout/index.tsx index 255ec88d9c..ee46647be0 100644 --- a/src/components/Layout/index.tsx +++ b/src/components/Layout/index.tsx @@ -4,6 +4,7 @@ import Header from "../Header" import "../../assets/css/style.less" import "../../assets/css/global.css" +import BackToTop from "../BackToTop" interface Props { children: React.ReactNode className?: string @@ -20,6 +21,7 @@ const IndexLayout = ({ {children}