1+ // Mobile Navigation
2+ document . addEventListener ( 'DOMContentLoaded' , function ( ) {
3+ // Create mobile navigation elements
4+ const mobileNavToggle = document . createElement ( 'div' ) ;
5+ mobileNavToggle . className = 'mobile-nav-toggle' ;
6+ mobileNavToggle . innerHTML = '<i class="fas fa-bars"></i>' ;
7+
8+ const mobileNav = document . createElement ( 'div' ) ;
9+ mobileNav . className = 'mobile-nav' ;
10+
11+ // Get all navigation links and clone them for mobile
12+ const navLinks = document . querySelectorAll ( '.nav-link' ) ;
13+ navLinks . forEach ( link => {
14+ const mobileLink = document . createElement ( 'a' ) ;
15+ mobileLink . className = 'mobile-nav-link' ;
16+ mobileLink . href = link . href ;
17+ mobileLink . textContent = link . textContent ;
18+ mobileNav . appendChild ( mobileLink ) ;
19+ } ) ;
20+
21+ // Add mobile navigation to the body
22+ document . body . appendChild ( mobileNavToggle ) ;
23+ document . body . appendChild ( mobileNav ) ;
24+
25+ // Toggle mobile navigation
26+ mobileNavToggle . addEventListener ( 'click' , function ( ) {
27+ mobileNav . classList . toggle ( 'active' ) ;
28+ mobileNavToggle . innerHTML = mobileNav . classList . contains ( 'active' )
29+ ? '<i class="fas fa-times"></i>'
30+ : '<i class="fas fa-bars"></i>' ;
31+ } ) ;
32+
33+ // Close mobile navigation when clicking a link
34+ document . querySelectorAll ( '.mobile-nav-link' ) . forEach ( link => {
35+ link . addEventListener ( 'click' , function ( ) {
36+ mobileNav . classList . remove ( 'active' ) ;
37+ mobileNavToggle . innerHTML = '<i class="fas fa-bars"></i>' ;
38+ } ) ;
39+ } ) ;
40+
41+ // Update active link on scroll for mobile
42+ window . addEventListener ( 'scroll' , ( ) => {
43+ let current = '' ;
44+ const scrollPosition = window . scrollY + 300 ;
45+
46+ document . querySelectorAll ( 'section' ) . forEach ( section => {
47+ const sectionTop = section . offsetTop ;
48+ const sectionHeight = section . clientHeight ;
49+
50+ if ( scrollPosition >= sectionTop && scrollPosition < sectionTop + sectionHeight ) {
51+ current = section . getAttribute ( 'id' ) ;
52+ }
53+ } ) ;
54+
55+ document . querySelectorAll ( '.mobile-nav-link' ) . forEach ( link => {
56+ link . classList . remove ( 'active' ) ;
57+ if ( link . getAttribute ( 'href' ) . slice ( 1 ) === current ) {
58+ link . classList . add ( 'active' ) ;
59+ }
60+ } ) ;
61+ } ) ;
62+
63+ // Hide mobile navigation on window resize if screen becomes larger
64+ window . addEventListener ( 'resize' , function ( ) {
65+ if ( window . innerWidth > 768 ) {
66+ mobileNav . classList . remove ( 'active' ) ;
67+ mobileNavToggle . innerHTML = '<i class="fas fa-bars"></i>' ;
68+ }
69+ } ) ;
70+ } ) ;
0 commit comments