diff --git a/src/components/Navbar copy.tsx b/src/components/Navbar copy.tsx new file mode 100644 index 0000000000000000000000000000000000000000..1bfee417e72a426336fa09c2da04eb49d49723f0 --- /dev/null +++ b/src/components/Navbar copy.tsx @@ -0,0 +1,94 @@ +import { useState, useEffect } from "react"; +import Nav from "react-bootstrap/Nav"; +import BootstrapNavbar from "react-bootstrap/Navbar"; +import NavDropdown from "react-bootstrap/NavDropdown"; +import { Link, useLocation } from "react-router-dom"; +import Pages from "../pages.ts"; +import { Container } from "react-bootstrap"; +import "./Navbar.css"; + +export function Navbar() { + const [scrollProgress, setScrollProgress] = useState(0); + const location = useLocation(); + + useEffect(() => { + const handleScroll = () => { + const totalScroll = document.documentElement.scrollHeight - window.innerHeight; + const currentScroll = window.pageYOffset; + const scrollPercentage = (currentScroll / totalScroll) * 100; + setScrollProgress(scrollPercentage); + }; + + if (location.pathname === "/") { + window.addEventListener("scroll", handleScroll); + handleScroll(); // åˆå§‹åŒ–进度 + } + + return () => { + window.removeEventListener("scroll", handleScroll); + }; + }, [location]); + const pages = Pages.map((item, pageIndex) => { + if ("folder" in item && item.folder) { + const folderItems = item.folder.map((subpage, subpageIndex) => { + if (subpage.path) { + const SubpageIcon = subpage.icon; // 获å–å页é¢å›¾æ ‡ + return ( + <NavDropdown.Item + key={`subpage-${pageIndex}-${subpageIndex}`} + as={Link} + to={subpage.path} + className="custom-dropdown-menu" + > + {SubpageIcon && <SubpageIcon />} {subpage.name} {/* æ·»åŠ å›¾æ ‡ */} + </NavDropdown.Item> + ); + } + }); + const ItemIcon = item.icon; // èŽ·å–æ–‡ä»¶å¤¹å›¾æ ‡ + return ( + <NavDropdown + key={`page-${pageIndex}`} + title={<>{ItemIcon && <ItemIcon />} {item.name}</>} // æ·»åŠ å›¾æ ‡ + id="basic-nav-dropdown" + className="custom-dropdown-menu" + > + {folderItems} + </NavDropdown> + ); + } else if ("path" in item && item.path) { + const ItemIcon = item.icon; // 获å–页é¢å›¾æ ‡ + return ( + <Nav.Link + key={`page-${pageIndex}`} as={Link} to={item.path} + className="custom-dropdown-menu" + > + {ItemIcon && <ItemIcon />} {item.name} {/* æ·»åŠ å›¾æ ‡ */} + </Nav.Link> + ); + } + }); + + return ( + + <> + <BootstrapNavbar expand="lg" className="apple-navbar" fixed="top"> + <Container fluid> + <Link to="/" className="apple-brand"> + <img src="https://static.igem.wiki/teams/5378/school-badge/liverbrainguardian.webp" className="apple-logo" alt="Logo" /> + </Link> + <BootstrapNavbar.Toggle aria-controls="basic-navbar-nav" className="apple-toggler" /> + <BootstrapNavbar.Collapse id="basic-navbar-nav"> + <Nav className="ms-auto apple-nav">{pages}</Nav> + </BootstrapNavbar.Collapse> + </Container> + </BootstrapNavbar> + {location.pathname === "/" && ( + <div + className="scroll-progress-bar" + style={{ width: `${scrollProgress}%` }} + ></div> + )} + </> + ); +} \ No newline at end of file diff --git a/src/components/Navbar.css b/src/components/Navbar.css index 0ba32f092e63ff59207fe76f7cf885b3dae52bb4..7386f51b0864c0ffa7aacaa4656bebb6816aef8a 100644 --- a/src/components/Navbar.css +++ b/src/components/Navbar.css @@ -68,8 +68,8 @@ position: fixed; top: 68px; /* 调整这个值以匹é…ä½ çš„å¯¼èˆªæ 高度 */ left: 0; - height: 3px; - background-color: #007aff; /* 使用 Apple é£Žæ ¼çš„è“色 */ + height: 7px; + background-color: #d9534f; /* 使用 Apple é£Žæ ¼çš„è“色 */ z-index: 1000; transition: width 0.1s ease-out; } diff --git a/src/components/Navbar.tsx b/src/components/Navbar.tsx index 1bfee417e72a426336fa09c2da04eb49d49723f0..102d5c1011b1a0e99a40b1625b9b29b684b5958e 100644 --- a/src/components/Navbar.tsx +++ b/src/components/Navbar.tsx @@ -11,6 +11,8 @@ export function Navbar() { const [scrollProgress, setScrollProgress] = useState(0); const location = useLocation(); + + useEffect(() => { const handleScroll = () => { const totalScroll = document.documentElement.scrollHeight - window.innerHeight; @@ -19,10 +21,9 @@ export function Navbar() { setScrollProgress(scrollPercentage); }; - if (location.pathname === "/") { - window.addEventListener("scroll", handleScroll); - handleScroll(); // åˆå§‹åŒ–进度 - } + // Remove the condition for the home page + window.addEventListener("scroll", handleScroll); + handleScroll(); // Initialize progress return () => { window.removeEventListener("scroll", handleScroll); @@ -83,12 +84,10 @@ export function Navbar() { </BootstrapNavbar.Collapse> </Container> </BootstrapNavbar> - {location.pathname === "/" && ( - <div - className="scroll-progress-bar" - style={{ width: `${scrollProgress}%` }} - ></div> - )} + <div + className="scroll-progress-bar" + style={{ width: `${scrollProgress}%` }} + ></div> </> ); } \ No newline at end of file