import { ArrowSmallUp } from '@/components/HeroIcons' import { useEffect, useState } from 'react' /** * 回顶按钮 * @returns */ export default function ReadingProgress() { const [scrollPercentage, setScrollPercentage] = useState(0) useEffect(() => { let requestId function handleScroll() { const scrollHeight = document.documentElement.scrollHeight const clientHeight = document.documentElement.clientHeight const scrollY = window.scrollY || window.pageYOffset const percent = Math.floor((scrollY / (scrollHeight - clientHeight)) * 100) setScrollPercentage(percent) requestId = requestAnimationFrame(handleScroll) } handleScroll() // 初始化滚动位置 return () => { cancelAnimationFrame(requestId) } }, []) return (<>
window.scrollTo({ top: 0, behavior: 'smooth' })} className={`${scrollPercentage > 0 ? 'w-10 h-10 ' : 'w-0 h-0 opacity-0'} group cursor-pointer hover:bg-black hover:bg-opacity-10 rounded-full flex justify-center items-center duration-200 transition-all`} >
) }