'use client'; import { useState, useEffect } from 'react'; import { motion, AnimatePresence } from 'framer-motion'; export default function ScrollToTop() { const [showScrollTop, setShowScrollTop] = useState(false); useEffect(() => { let ticking = false; const handleScroll = () => { if (!ticking) { window.requestAnimationFrame(() => { const currentScrollY = window.scrollY; // 显示返回顶部按钮(滚动超过300px) setShowScrollTop(currentScrollY > 300); ticking = false; }); ticking = true; } }; window.addEventListener('scroll', handleScroll, { passive: true }); return () => window.removeEventListener('scroll', handleScroll); }, []); const scrollToTop = () => { window.scrollTo({ top: 0, behavior: 'smooth' }); }; return ( {showScrollTop && ( )} ); }