Repository navigation
Expand file tree
/
Copy pathscript.js
More file actions
222 lines (189 loc) · 6.53 KB
/
Copy pathscript.js
File metadata and controls
222 lines (189 loc) · 6.53 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/* =============================================
Shuaib Ahamad — Portfolio JavaScript
============================================= */
// ===== DYNAMIC TEXT TYPING (Hero Subtitle) =====
const roles = [
'Aspiring Cybersecurity Professional',
'Hardware Maker & Developer',
];
const dynamicEl = document.getElementById('dynamicText');
let roleIndex = 0;
let charIndex = 0;
let isDeleting = false;
let typeSpeed = 80;
function typeEffect() {
if (!dynamicEl) return;
const currentRole = roles[roleIndex];
if (isDeleting) {
dynamicEl.textContent = currentRole.substring(0, charIndex - 1);
charIndex--;
typeSpeed = 40;
} else {
dynamicEl.textContent = currentRole.substring(0, charIndex + 1);
charIndex++;
typeSpeed = 80;
}
if (!isDeleting && charIndex === currentRole.length) {
isDeleting = true;
typeSpeed = 1500; // pause before deleting
} else if (isDeleting && charIndex === 0) {
isDeleting = false;
roleIndex = (roleIndex + 1) % roles.length;
typeSpeed = 400; // pause before typing next
}
setTimeout(typeEffect, typeSpeed);
}
// Start typing effect
if (dynamicEl) {
document.addEventListener('DOMContentLoaded', () => {
setTimeout(typeEffect, 1000);
});
}
// ===== MOBILE HAMBURGER MENU =====
const hamburger = document.getElementById('hamburger');
const navLinks = document.getElementById('navLinks');
if (hamburger && navLinks) {
hamburger.addEventListener('click', () => {
hamburger.classList.toggle('active');
navLinks.classList.toggle('open');
});
// Close mobile menu when a link is clicked
document.querySelectorAll('.nav-link').forEach(link => {
link.addEventListener('click', () => {
hamburger.classList.remove('active');
navLinks.classList.remove('open');
});
});
}
// ===== ACTIVE NAV LINK ON SCROLL =====
const sections = document.querySelectorAll('section[id]');
const navLinkEls = document.querySelectorAll('.nav-link');
function updateActiveLink() {
let current = '';
const scrollPos = window.scrollY + 100;
sections.forEach(section => {
const top = section.offsetTop;
const height = section.offsetHeight;
if (scrollPos >= top && scrollPos < top + height) {
current = section.getAttribute('id');
}
});
navLinkEls.forEach(link => {
link.classList.remove('active');
if (link.getAttribute('href') === `#${current}`) {
link.classList.add('active');
}
});
}
window.addEventListener('scroll', updateActiveLink);
// ===== SCROLL-BASED NAVBAR SHADOW =====
const navbar = document.getElementById('navbar');
window.addEventListener('scroll', () => {
if (window.scrollY > 50) {
navbar.classList.add('scrolled');
} else {
navbar.classList.remove('scrolled');
}
});
// ===== SCROLL REVEAL ANIMATIONS (Intersection Observer) =====
const revealElements = document.querySelectorAll(
'.section-title, .divider, .about-content, .skills-category, ' +
'.project-card, .contact-content, .contact-intro, .hero-content > *'
);
const observer = new IntersectionObserver(
(entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('visible');
}
});
},
{ threshold: 0.1 }
);
revealElements.forEach(el => {
// Skip hero children — they have their own animation
if (!el.closest('.hero')) {
el.classList.add('fade-in');
observer.observe(el);
}
});
// ===== CONTACT FORM HANDLING (Formspree) =====
const contactForm = document.getElementById('contactForm');
const FORMSPREE_ENDPOINT = 'https://formspree.io/f/mkjwadza';
if (contactForm) contactForm.addEventListener('submit', async (e) => {
e.preventDefault();
const name = document.getElementById('formName').value.trim();
const email = document.getElementById('formEmail').value.trim();
const message = document.getElementById('formMessage').value.trim();
// Clear any previous status message
const existingStatus = contactForm.querySelector('.form-status');
if (existingStatus) existingStatus.remove();
if (!name || !email || !message) {
showFormStatus('Please fill in all fields.', false);
return;
}
// Simple email validation
if (!email.includes('@') || !email.includes('.')) {
showFormStatus('Please enter a valid email address.', false);
return;
}
const submitBtn = contactForm.querySelector('button[type="submit"]');
const originalHTML = submitBtn.innerHTML;
submitBtn.disabled = true;
submitBtn.innerHTML = 'Sending...';
try {
const response = await fetch(FORMSPREE_ENDPOINT, {
method: 'POST',
headers: { 'Content-Type': 'application/json', 'Accept': 'application/json' },
body: JSON.stringify({
name,
email,
message,
// Formspree special fields: reply-to goes to the submitter, subject customizes the email subject
_replyto: email,
_subject: `Portfolio message from ${name}`
})
});
if (response.ok) {
// Show success message (replaces the form)
contactForm.innerHTML = `
<div class="form-success form-status">
<svg class="icon"><use href="#i-check-circle"/></svg>
<p>Thank you, <strong>${name}</strong>! Your message has been sent.</p>
<p style="color: var(--text-secondary); font-size: 0.9rem; margin-top: 8px;">
I'll get back to you soon.
</p>
</div>
`;
} else {
const data = await response.json().catch(() => null);
const errMsg = data && data.errors && data.errors[0] ? data.errors[0].message : 'Something went wrong. Please try again.';
showFormStatus(errMsg, false);
submitBtn.disabled = false;
submitBtn.innerHTML = originalHTML;
}
} catch (error) {
showFormStatus('Network error. Please check your connection and try again.', false);
submitBtn.disabled = false;
submitBtn.innerHTML = originalHTML;
}
});
// Helper to show an inline status message (error or info)
function showFormStatus(msg, isSuccess) {
const status = document.createElement('div');
status.className = 'form-status' + (isSuccess ? ' success' : ' error');
status.textContent = msg;
contactForm.insertBefore(status, contactForm.querySelector('button[type="submit"]'));
}
// ===== SMOOTH SCROLL FOR ANCHOR LINKS (fallback) =====
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
const href = this.getAttribute('href');
if (href === '#') return;
e.preventDefault();
const target = document.querySelector(href);
if (target) {
target.scrollIntoView({ behavior: 'smooth' });
}
});
});