diff --git a/src/components/GoToTop.css b/src/components/GoToTop.css new file mode 100644 index 0000000000000000000000000000000000000000..145f97f8ded7cd354bc54e23d7a87648833cb218 --- /dev/null +++ b/src/components/GoToTop.css @@ -0,0 +1,24 @@ +/* GoToTop.css */ +.go-to-top-button { + position: fixed; + bottom: 30px; + right: 30px; + padding: 10px 20px; + font-size: 16px; + background-color: #ff69b4; /* å¯çˆ±çš„粉红色 */ + color: white; + border: none; + border-radius: 50px; + cursor: pointer; + box-shadow: 0px 4px 8px rgba(0, 0, 0, 0.1); + opacity: 0.8; + transition: opacity 0.3s ease; + } + + .go-to-top-button:hover { + opacity: 1; /* é¼ æ ‡æ‚¬åœæ—¶å˜å¾—更清晰 */ + } + + .go-to-top-button:active { + background-color: #ff1493; /* 点击时颜色å˜æ·± */ + } \ No newline at end of file diff --git a/src/components/GoToTop.tsx b/src/components/GoToTop.tsx new file mode 100644 index 0000000000000000000000000000000000000000..5cea98e83ed13e1627f7e560d2bff8678e69793d --- /dev/null +++ b/src/components/GoToTop.tsx @@ -0,0 +1,42 @@ +import { useState, useEffect } from 'react'; +import './GoToTop.css'; + +export function GoToTop(){ + const [showButton, setShowButton] = useState(false); + + // 检测页é¢æ»šåŠ¨ + useEffect(() => { + const handleScroll = () => { + if (window.scrollY > 300) { + setShowButton(true); // 当滚动超过300px时,显示按钮 + } else { + setShowButton(false); // 滚动回顶部时éšè—按钮 + } + }; + + window.addEventListener('scroll', handleScroll); + return () => { + window.removeEventListener('scroll', handleScroll); + }; + }, []); + + // 点击按钮滚动到顶部 + const scrollToTop = () => { + window.scrollTo({ + top: 0, + behavior: 'smooth' // 平滑滚动 + }); + }; + + return ( + <> + {showButton && ( + <button className="go-to-top-button" onClick={scrollToTop}> + ↑ Go to Top + </button> + )} + </> + ); +}; + + diff --git a/src/containers/App/App.tsx b/src/containers/App/App.tsx index e7d0cc3c22983952824928ccbe641aacc138d510..a2233365082992b5a6e0a33832253952e51c3475 100644 --- a/src/containers/App/App.tsx +++ b/src/containers/App/App.tsx @@ -2,7 +2,7 @@ import "./App.css"; import "bootstrap/dist/css/bootstrap.min.css"; import 'bootstrap/dist/js/bootstrap.bundle.min.js'; // 引入 Bootstrap JavaScript import { Route, Routes, useLocation } from "react-router-dom"; -import { Footer, Header, Navbar, NotFound, Loading } from "../../components"; +import { Footer, Header, Navbar, NotFound, Loading,} from "../../components"; import { getPathMapping, stringToSlug } from "../../utils"; import { useEffect, useState } from "react"; import axios from "axios";