import React, { useState, useEffect } from 'react'; import { ChevronUp } from 'lucide-react'; import { cn } from '../../lib/utils'; interface BackToTopProps { className?: string; threshold?: number; // 滚动多少像素后显示按钮 } const BackToTop: React.FC = ({ className, threshold = 300 }) => { const [isVisible, setIsVisible] = useState(false); // 监听滚动事件 useEffect(() => { const toggleVisibility = () => { if (window.pageYOffset > threshold) { setIsVisible(true); } else { setIsVisible(false); } }; window.addEventListener('scroll', toggleVisibility); return () => { window.removeEventListener('scroll', toggleVisibility); }; }, [threshold]); // 滚动到顶部 const scrollToTop = () => { window.scrollTo({ top: 0, behavior: 'smooth' }); }; if (!isVisible) { return null; } return ( ); }; export default BackToTop; export { BackToTop };