-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
439 lines (365 loc) · 12.4 KB
/
Copy pathscript.js
File metadata and controls
439 lines (365 loc) · 12.4 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
/* ==========================================
PORTFOLIO WEBSITE - JAVASCRIPT
Handles navigation, animations, and interactions
========================================== */
// ==========================================
// DOM ELEMENTS
// ==========================================
const navbar = document.getElementById('navbar');
const hamburger = document.getElementById('hamburger');
const navMenu = document.getElementById('nav-menu');
const navLinks = document.querySelectorAll('.nav-link');
const themeToggle = document.getElementById('theme-toggle');
const backToTopBtn = document.getElementById('back-to-top');
const contactForm = document.getElementById('contact-form');
// ==========================================
// INITIALIZATION
// ==========================================
document.addEventListener('DOMContentLoaded', () => {
initTheme();
setupEventListeners();
setupScrollAnimations();
});
// ==========================================
// THEME MANAGEMENT (Dark Mode)
// ==========================================
function initTheme() {
// Check for saved theme preference or default to light mode
const savedTheme = localStorage.getItem('theme') || 'light-mode';
document.body.classList.add(savedTheme);
updateThemeIcon();
}
function toggleTheme() {
document.body.classList.toggle('dark-mode');
const isDarkMode = document.body.classList.contains('dark-mode');
localStorage.setItem('theme', isDarkMode ? 'dark-mode' : 'light-mode');
updateThemeIcon();
}
function updateThemeIcon() {
const isDarkMode = document.body.classList.contains('dark-mode');
themeToggle.textContent = isDarkMode ? '☀️' : '🌙';
}
// ==========================================
// NAVIGATION
// ==========================================
function setupEventListeners() {
// Hamburger menu toggle
hamburger.addEventListener('click', toggleMobileMenu);
// Navigation links
navLinks.forEach(link => {
link.addEventListener('click', (e) => {
// Don't close menu for theme toggle button
if (e.target.closest('.theme-toggle')) {
toggleTheme();
return;
}
closeMobileMenu();
updateActiveNav(e.target);
});
});
// Theme toggle
themeToggle.addEventListener('click', (e) => {
e.preventDefault();
toggleTheme();
});
// Back to top button
backToTopBtn.addEventListener('click', scrollToTop);
// Contact form
if (contactForm) {
contactForm.addEventListener('submit', handleFormSubmit);
}
// Scroll events
window.addEventListener('scroll', () => {
handleNavbarOnScroll();
showBackToTopButton();
handleScrollAnimations();
});
}
function toggleMobileMenu() {
hamburger.classList.toggle('active');
navMenu.classList.toggle('active');
}
function closeMobileMenu() {
hamburger.classList.remove('active');
navMenu.classList.remove('active');
}
// ==========================================
// ACTIVE NAVIGATION
// ==========================================
function updateActiveNav(element) {
// Remove active class from all links
navLinks.forEach(link => link.classList.remove('active'));
// Add active class to clicked link
if (element.classList.contains('nav-link')) {
element.classList.add('active');
}
}
// Update active nav based on scroll position
function updateActiveNavOnScroll() {
const sections = document.querySelectorAll('section');
let currentSection = '';
sections.forEach(section => {
const sectionTop = section.offsetTop;
const sectionHeight = section.clientHeight;
if (window.pageYOffset >= sectionTop - 200) {
currentSection = section.getAttribute('id');
}
});
navLinks.forEach(link => {
link.classList.remove('active');
if (link.getAttribute('href') === `#${currentSection}`) {
link.classList.add('active');
}
});
}
// ==========================================
// NAVBAR EFFECTS
// ==========================================
let lastScrollY = 0;
function handleNavbarOnScroll() {
const scrollY = window.pageYOffset;
// Add shadow on scroll
if (scrollY > 10) {
navbar.style.boxShadow = '0 5px 20px rgba(0, 0, 0, 0.1)';
} else {
navbar.style.boxShadow = '0 2px 10px rgba(0, 0, 0, 0.05)';
}
// Update active navigation
updateActiveNavOnScroll();
lastScrollY = scrollY;
}
// ==========================================
// BACK TO TOP BUTTON
// ==========================================
function showBackToTopButton() {
if (window.pageYOffset > 300) {
backToTopBtn.classList.add('show');
} else {
backToTopBtn.classList.remove('show');
}
}
function scrollToTop() {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
}
// ==========================================
// FORM HANDLING
// ==========================================
function handleFormSubmit(e) {
e.preventDefault();
const formData = {
name: document.getElementById('name').value.trim(),
email: document.getElementById('email').value.trim(),
subject: document.getElementById('subject').value.trim(),
message: document.getElementById('message').value.trim()
};
// Validate form data
if (!validateForm(formData)) {
showFormMessage('Please fill in all fields correctly.', 'error');
return;
}
// Simulate form submission
const submitBtn = contactForm.querySelector('.btn-submit');
const originalText = submitBtn.textContent;
submitBtn.textContent = 'Sending...';
submitBtn.disabled = true;
// Simulate API call
setTimeout(() => {
showFormMessage(
'Thank you! Your message has been received. I\'ll get back to you soon.',
'success'
);
contactForm.reset();
submitBtn.textContent = originalText;
submitBtn.disabled = false;
}, 1500);
}
function validateForm(data) {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return (
data.name.length > 0 &&
data.email.length > 0 &&
emailRegex.test(data.email) &&
data.subject.length > 0 &&
data.message.length > 0
);
}
function showFormMessage(message, type) {
// Remove existing message if present
const existingMessage = contactForm.querySelector('.form-message');
if (existingMessage) {
existingMessage.remove();
}
const messageDiv = document.createElement('div');
messageDiv.className = `form-message form-message-${type}`;
messageDiv.textContent = message;
messageDiv.style.cssText = `
padding: 12px 16px;
border-radius: 6px;
margin-bottom: 16px;
font-weight: 500;
${type === 'success'
? 'background-color: rgba(34, 197, 94, 0.1); color: #22c55e; border: 1px solid #22c55e;'
: 'background-color: rgba(239, 68, 68, 0.1); color: #ef4444; border: 1px solid #ef4444;'
}
`;
contactForm.insertBefore(messageDiv, contactForm.firstChild);
// Remove message after 5 seconds
setTimeout(() => {
messageDiv.remove();
}, 5000);
}
// ==========================================
// SCROLL ANIMATIONS
// ==========================================
function setupScrollAnimations() {
const observerOptions = {
threshold: 0.1,
rootMargin: '0px 0px -100px 0px'
};
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('scroll-animate');
observer.unobserve(entry.target);
}
});
}, observerOptions);
// Observe elements that should animate on scroll
const animateOnScroll = document.querySelectorAll(
'.skill-category, .project-card, .achievement-card, .timeline-item, .stat-card'
);
animateOnScroll.forEach(element => {
element.style.opacity = '0';
element.style.transform = 'translateY(30px)';
observer.observe(element);
});
}
function handleScrollAnimations() {
// Additional scroll animation logic can be added here
}
// ==========================================
// SMOOTH SCROLLING FOR ANCHOR LINKS
// ==========================================
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
const href = this.getAttribute('href');
// Skip if it's the theme toggle or hamburger
if (this.closest('.theme-toggle') || this.closest('.hamburger')) {
return;
}
// Skip if href is just '#'
if (href === '#') {
return;
}
e.preventDefault();
const target = document.querySelector(href);
if (target) {
const offsetTop = target.offsetTop - 70; // Account for navbar height
window.scrollTo({
top: offsetTop,
behavior: 'smooth'
});
// Update active nav if it's a section link
if (target.tagName === 'SECTION') {
const navLink = document.querySelector(`a[href="${href}"]`);
if (navLink) {
updateActiveNav(navLink);
}
}
}
});
});
// ==========================================
// UTILITY FUNCTIONS
// ==========================================
/**
* Debounce function to limit function calls
* @param {Function} func - The function to debounce
* @param {number} wait - The debounce delay in milliseconds
* @returns {Function} The debounced function
*/
function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
/**
* Throttle function to limit function calls
* @param {Function} func - The function to throttle
* @param {number} limit - The throttle limit in milliseconds
* @returns {Function} The throttled function
*/
function throttle(func, limit) {
let inThrottle;
return function(...args) {
if (!inThrottle) {
func.apply(this, args);
inThrottle = true;
setTimeout(() => inThrottle = false, limit);
}
};
}
// ==========================================
// KEYBOARD NAVIGATION
// ==========================================
document.addEventListener('keydown', (e) => {
// Close mobile menu on Escape
if (e.key === 'Escape') {
closeMobileMenu();
}
// Back to top on Control + Home
if (e.ctrlKey && e.key === 'Home') {
scrollToTop();
}
});
// ==========================================
// RESPONSIVE BEHAVIOR
// ==========================================
// Close mobile menu on window resize if screen becomes larger
let resizeTimer;
window.addEventListener('resize', debounce(() => {
if (window.innerWidth > 768) {
closeMobileMenu();
}
}, 250));
// ==========================================
// PREFERS REDUCED MOTION
// ==========================================
// Respect user's motion preferences
const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
if (prefersReducedMotion) {
// Apply reduced motion styles
document.querySelectorAll('*').forEach(el => {
el.style.animationDuration = '0.01ms !important';
el.style.transitionDuration = '0.01ms !important';
});
}
// ==========================================
// PERFORMANCE MONITORING
// ==========================================
// Log performance metrics (optional, for debugging)
if (window.performance && window.performance.timing) {
window.addEventListener('load', () => {
const perfData = window.performance.timing;
const pageLoadTime = perfData.loadEventEnd - perfData.navigationStart;
console.log(`Page loaded in ${pageLoadTime}ms`);
});
}
// ==========================================
// SERVICE WORKER REGISTRATION (Optional for PWA)
// ==========================================
// Uncomment to enable service worker for offline support
// if ('serviceWorker' in navigator) {
// navigator.serviceWorker.register('/sw.js').catch(err => {
// console.log('Service Worker registration failed:', err);
// });
// }