-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
51 lines (43 loc) · 1.8 KB
/
script.js
File metadata and controls
51 lines (43 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Mobile menu toggle functionality
document.addEventListener('DOMContentLoaded', () => {
const menuToggle = document.querySelector('.menu-toggle');
const navLinks = document.querySelector('.nav-links');
const navLinksItems = document.querySelectorAll('.nav-links a');
// Toggle menu on hamburger click
menuToggle.addEventListener('click', () => {
navLinks.classList.toggle('active');
// Update aria-expanded attribute for accessibility
const isExpanded = navLinks.classList.contains('active');
menuToggle.setAttribute('aria-expanded', isExpanded);
});
// Close menu when a nav link is clicked
navLinksItems.forEach(link => {
link.addEventListener('click', () => {
navLinks.classList.remove('active');
menuToggle.setAttribute('aria-expanded', false);
});
});
// Close menu when clicking outside
document.addEventListener('click', (e) => {
const isNavLinks = navLinks.contains(e.target);
const isMenuToggle = menuToggle.contains(e.target);
if (!isNavLinks && !isMenuToggle && navLinks.classList.contains('active')) {
navLinks.classList.remove('active');
menuToggle.setAttribute('aria-expanded', false);
}
});
});
// Smooth scroll for navigation
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function(e) {
e.preventDefault();
const targetId = this.getAttribute('href');
const targetElement = document.querySelector(targetId);
if (targetElement) {
window.scrollTo({
top: targetElement.offsetTop - 70, // Adjust for navbar height
behavior: 'smooth'
});
}
});
});