diff --git a/src/components/layout/header.tsx b/src/components/layout/header.tsx index 3f8bf08..66c69e3 100644 --- a/src/components/layout/header.tsx +++ b/src/components/layout/header.tsx @@ -1,6 +1,8 @@ 'use client'; import { useState, useEffect, useCallback, useRef } from 'react'; +import Link from 'next/link'; +import { usePathname } from 'next/navigation'; import { Menu, X } from 'lucide-react'; import { motion, AnimatePresence } from 'framer-motion'; import { Button } from '@/components/ui/button'; @@ -9,87 +11,13 @@ import { useFocusTrap } from '@/hooks/use-focus-trap'; export function Header() { const [isOpen, setIsOpen] = useState(false); - const [activeSection, setActiveSection] = useState('home'); const [isScrolled, setIsScrolled] = useState(false); - const isScrollingRef = useRef(false); - const scrollTimeoutRef = useRef(null); + const pathname = usePathname(); const focusTrapRef = useFocusTrap(isOpen); - const handleNavClick = useCallback((e: React.MouseEvent, href: string) => { - e.preventDefault(); - const targetId = href.replace('#', ''); - const element = document.getElementById(targetId); - - if (element) { - isScrollingRef.current = true; - - if (scrollTimeoutRef.current) { - clearTimeout(scrollTimeoutRef.current); - } - - const headerOffset = 64; - const elementPosition = element.getBoundingClientRect().top; - const offsetPosition = elementPosition + window.pageYOffset - headerOffset; - - window.scrollTo({ - top: offsetPosition, - behavior: 'smooth' - }); - - setActiveSection(targetId); - setIsOpen(false); - - scrollTimeoutRef.current = setTimeout(() => { - isScrollingRef.current = false; - }, 1000); - } - }, []); - - const handleKeyDown = useCallback((e: React.KeyboardEvent, href?: string) => { - if (e.key === 'Enter' || e.key === ' ') { - e.preventDefault(); - if (href) { - const targetId = href.replace('#', ''); - const element = document.getElementById(targetId); - if (element) { - element.scrollIntoView({ behavior: 'smooth' }); - setActiveSection(targetId); - setIsOpen(false); - } - } else { - setIsOpen(!isOpen); - } - } - if (e.key === 'Escape' && isOpen) { - setIsOpen(false); - } - }, [isOpen]); - useEffect(() => { const handleScroll = () => { setIsScrolled(window.scrollY > 20); - - if (isScrollingRef.current) { - return; - } - - const sections = NAVIGATION.map(item => item.href.replace('#', '')); - const scrollPosition = window.scrollY + 100; - - for (let i = sections.length - 1; i >= 0; i--) { - const sectionId = sections[i]; - if (!sectionId) {continue;} - - const section = document.getElementById(sectionId); - if (section) { - const sectionTop = section.offsetTop; - const sectionBottom = sectionTop + section.offsetHeight; - if (scrollPosition >= sectionTop && scrollPosition < sectionBottom) { - setActiveSection(sectionId); - break; - } - } - } }; window.addEventListener('scroll', handleScroll, { passive: true }); @@ -97,12 +25,19 @@ export function Header() { return () => { window.removeEventListener('scroll', handleScroll); - if (scrollTimeoutRef.current) { - clearTimeout(scrollTimeoutRef.current); - } }; }, []); + const handleKeyDown = useCallback((e: React.KeyboardEvent) => { + if (e.key === 'Enter' || e.key === ' ') { + e.preventDefault(); + setIsOpen(!isOpen); + } + if (e.key === 'Escape' && isOpen) { + setIsOpen(false); + } + }, [isOpen]); + return ( <>
- handleNavClick(e, '#home')} - onKeyDown={(e) => handleKeyDown(e, '#home')} + {COMPANY_INFO.name} - +
diff --git a/src/lib/constants.ts b/src/lib/constants.ts index 54ed68b..ab3ea29 100644 --- a/src/lib/constants.ts +++ b/src/lib/constants.ts @@ -11,14 +11,14 @@ export const COMPANY_INFO = { address: '中国四川省成都市龙泉驿区幸福路12号', } as const; -// Navigation Items - 单页面锚点导航(顺序必须与页面 Section 顺序一致) +// Navigation Items - 独立页面导航 export const NAVIGATION = [ - { id: 'home', label: '首页', href: '#home' }, - { id: 'solutions', label: '核心业务', href: '/solutions' }, - { id: 'products', label: '产品服务', href: '#products' }, - { id: 'about', label: '关于我们', href: '#about' }, - { id: 'news', label: '新闻动态', href: '#news' }, - { id: 'contact', label: '联系我们', href: '#contact' }, + { id: 'home', label: '首页', href: '/' }, + { id: 'services', label: '核心业务', href: '/services' }, + { id: 'products', label: '产品服务', href: '/products' }, + { id: 'about', label: '关于我们', href: '/about' }, + { id: 'news', label: '新闻动态', href: '/news' }, + { id: 'contact', label: '联系我们', href: '/contact' }, ] as const; // Stats Data