-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
76 lines (63 loc) · 2.49 KB
/
script.js
File metadata and controls
76 lines (63 loc) · 2.49 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
// Mobile nav toggle
const navToggleButton = document.querySelector('.nav__toggle');
const navMenu = document.getElementById('navMenu');
if (navToggleButton && navMenu) {
navToggleButton.addEventListener('click', () => {
const expanded = navToggleButton.getAttribute('aria-expanded') === 'true';
navToggleButton.setAttribute('aria-expanded', String(!expanded));
navMenu.classList.toggle('is-open');
});
}
// Close menu on link click (mobile)
document.querySelectorAll('.nav__link').forEach(link => {
link.addEventListener('click', () => {
if (navMenu && navMenu.classList.contains('is-open')) {
navMenu.classList.remove('is-open');
navToggleButton?.setAttribute('aria-expanded', 'false');
}
});
});
// Scrollspy active link highlighting
const sectionIds = ['home', 'about', 'skills', 'projects', 'contact'];
const sectionMap = new Map(
sectionIds.map(id => [id, document.getElementById(id)])
);
const linkMap = new Map(
Array.from(document.querySelectorAll('.nav__link')).map(a => [a.getAttribute('href')?.replace('#', ''), a])
);
const observer = new IntersectionObserver(
entries => {
entries.forEach(entry => {
const id = entry.target.getAttribute('id');
const link = id ? linkMap.get(id) : null;
if (!link) return;
if (entry.isIntersecting) {
document.querySelectorAll('.nav__link').forEach(a => a.classList.remove('is-active'));
link.classList.add('is-active');
}
});
},
{ rootMargin: '-40% 0px -55% 0px', threshold: [0, 0.25, 0.5, 1] }
);
sectionMap.forEach(sec => sec && observer.observe(sec));
// Footer year
const yearSpan = document.getElementById('year');
if (yearSpan) yearSpan.textContent = new Date().getFullYear().toString();
// Contact form handler via mailto
const contactForm = document.getElementById('contactForm');
if (contactForm) {
contactForm.addEventListener('submit', e => {
e.preventDefault();
const formData = new FormData(contactForm);
const name = (formData.get('name') || '').toString().trim();
const email = (formData.get('email') || '').toString().trim();
const message = (formData.get('message') || '').toString().trim();
if (!name || !email || !message) {
alert('Please fill out all fields.');
return;
}
const subject = encodeURIComponent(`Portfolio Contact from ${name}`);
const body = encodeURIComponent(`Name: ${name}\nEmail: ${email}\n\n${message}`);
window.location.href = `mailto:vasu@example.com?subject=${subject}&body=${body}`;
});
}