1+
2+ document . addEventListener ( "DOMContentLoaded" , function ( ) {
3+ const navbarLinks = document . querySelectorAll ( ".nav-links a" ) ;
4+ const footerLinks = document . querySelectorAll ( ".footer-links a" ) ;
5+ const navbarHeight = 50 ;
6+
7+ const allLinks = [ ...navbarLinks , ...footerLinks ] ;
8+
9+ allLinks . forEach ( link => {
10+ link . addEventListener ( "click" , function ( e ) {
11+ smoothScroll ( e , allLinks ) ;
12+ } ) ;
13+ } ) ;
14+
15+ function smoothScroll ( e , links ) {
16+ e . preventDefault ( ) ;
17+
18+ const targetHref = e . target . getAttribute ( "href" ) ;
19+ if ( targetHref . startsWith ( "#" ) ) {
20+ const targetId = targetHref . slice ( 1 ) ;
21+ const targetSection = document . getElementById ( targetId ) ;
22+
23+ if ( targetSection ) {
24+ const targetPosition = targetSection . offsetTop - navbarHeight ;
25+ window . scrollTo ( {
26+ top : targetPosition ,
27+ behavior : "smooth"
28+ } ) ;
29+
30+ links . forEach ( nav => nav . classList . remove ( "active" ) ) ;
31+ e . target . classList . add ( "active" ) ;
32+ }
33+ }
34+ }
35+ } ) ;
36+ function toggleMenu ( ) {
37+ const navLinks = document . querySelector ( '.nav-links' ) ;
38+ const hamburgerButton = document . querySelector ( '.hamburger-button' ) ;
39+
40+ // Toggle the 'open' class for nav links
41+ navLinks . classList . toggle ( 'open' ) ;
42+
43+ // Update the hamburger icon based on state
44+ if ( navLinks . classList . contains ( 'open' ) ) {
45+ hamburgerButton . innerHTML = '✕' ; // Change to 'X' when open
46+ } else {
47+ hamburgerButton . innerHTML = '☰' ; // Change back to hamburger icon
48+ }
49+ }
50+
51+ // Close the menu when clicking outside
52+ document . addEventListener ( 'click' , ( event ) => {
53+ const navLinks = document . querySelector ( '.nav-links' ) ;
54+ const hamburgerButton = document . querySelector ( '.hamburger-button' ) ;
55+
56+ // Check if the click is outside the menu and hamburger button
57+ if ( ! navLinks . contains ( event . target ) && ! hamburgerButton . contains ( event . target ) ) {
58+ navLinks . classList . remove ( 'open' ) ;
59+ hamburgerButton . innerHTML = '☰' ; // Reset to hamburger icon
60+ }
61+ } ) ;
62+
0 commit comments