-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
538 lines (448 loc) · 18.1 KB
/
script.js
File metadata and controls
538 lines (448 loc) · 18.1 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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
// Portfolio Website JavaScript
// Handles navigation, animations, form submission, and interactive features
document.addEventListener('DOMContentLoaded', function() {
// Navigation functionality
const navbar = document.getElementById('navbar');
const navToggle = document.getElementById('nav-toggle');
const navMenu = document.getElementById('nav-menu');
const navLinks = document.querySelectorAll('.nav-link');
// Mobile menu toggle
navToggle.addEventListener('click', function() {
navMenu.classList.toggle('active');
navToggle.classList.toggle('active');
});
// Close mobile menu when clicking on nav links
navLinks.forEach(link => {
link.addEventListener('click', function() {
navMenu.classList.remove('active');
navToggle.classList.remove('active');
});
});
// Navbar scroll effect
window.addEventListener('scroll', function() {
if (window.scrollY > 50) {
navbar.classList.add('scrolled');
} else {
navbar.classList.remove('scrolled');
}
});
// Active navigation link highlighting
function updateActiveNavLink() {
const sections = document.querySelectorAll('section[id]');
const scrollPos = window.scrollY + 100;
sections.forEach(section => {
const top = section.offsetTop;
const bottom = top + section.offsetHeight;
const id = section.getAttribute('id');
if (scrollPos >= top && scrollPos <= bottom) {
navLinks.forEach(link => link.classList.remove('active'));
const activeLink = document.querySelector(`.nav-link[href="#${id}"]`);
if (activeLink) {
activeLink.classList.add('active');
}
}
});
}
window.addEventListener('scroll', updateActiveNavLink);
// Smooth scrolling for navigation links
navLinks.forEach(link => {
link.addEventListener('click', function(e) {
e.preventDefault();
const targetId = this.getAttribute('href').substring(1);
const targetSection = document.getElementById(targetId);
if (targetSection) {
const offsetTop = targetSection.offsetTop - 70;
window.scrollTo({
top: offsetTop,
behavior: 'smooth'
});
}
});
});
// Typing animation for hero title
function typeWriter() {
const typingElement = document.querySelector('.typing-text');
if (!typingElement) return;
const text = "Hi, I'm Abc Singh";
let i = 0;
function type() {
if (i < text.length) {
typingElement.textContent = text.substring(0, i + 1);
i++;
setTimeout(type, 100);
}
}
setTimeout(type, 1000);
}
typeWriter();
// Counter animation for statistics
function animateCounters() {
const counters = document.querySelectorAll('.stat-number');
const observerOptions = {
threshold: 0.5,
rootMargin: '0px 0px -100px 0px'
};
const observer = new IntersectionObserver(function(entries) {
entries.forEach(entry => {
if (entry.isIntersecting) {
const counter = entry.target;
const target = parseInt(counter.getAttribute('data-target'));
const increment = target / 50;
let current = 0;
const updateCounter = () => {
if (current < target) {
current += increment;
counter.textContent = Math.ceil(current);
setTimeout(updateCounter, 40);
} else {
counter.textContent = target;
}
};
updateCounter();
observer.unobserve(counter);
}
});
}, observerOptions);
counters.forEach(counter => observer.observe(counter));
}
animateCounters();
// Skills bar animation
function animateSkillBars() {
const skillBars = document.querySelectorAll('.skill-progress');
const observerOptions = {
threshold: 0.5,
rootMargin: '0px 0px -100px 0px'
};
const observer = new IntersectionObserver(function(entries) {
entries.forEach(entry => {
if (entry.isIntersecting) {
const skillBar = entry.target;
const width = skillBar.getAttribute('data-width');
setTimeout(() => {
skillBar.style.width = width + '%';
}, 300);
observer.unobserve(skillBar);
}
});
}, observerOptions);
skillBars.forEach(bar => observer.observe(bar));
}
animateSkillBars();
// Portfolio filtering
function initPortfolioFilter() {
const filterBtns = document.querySelectorAll('.filter-btn');
const portfolioItems = document.querySelectorAll('.portfolio-item');
filterBtns.forEach(btn => {
btn.addEventListener('click', function() {
// Remove active class from all buttons
filterBtns.forEach(b => b.classList.remove('active'));
// Add active class to clicked button
this.classList.add('active');
const filterValue = this.getAttribute('data-filter');
portfolioItems.forEach(item => {
if (filterValue === 'all') {
item.style.display = 'block';
setTimeout(() => {
item.classList.add('visible');
item.classList.remove('hidden');
}, 10);
} else {
const category = item.getAttribute('data-category');
if (category === filterValue) {
item.style.display = 'block';
setTimeout(() => {
item.classList.add('visible');
item.classList.remove('hidden');
}, 10);
} else {
item.classList.remove('visible');
item.classList.add('hidden');
setTimeout(() => {
item.style.display = 'none';
}, 300);
}
}
});
});
});
}
initPortfolioFilter();
// Fade in animation on scroll
function initScrollAnimations() {
const fadeElements = document.querySelectorAll('.about-text, .skill-item, .tool-item, .contact-info, .contact-form');
const observerOptions = {
threshold: 0.1,
rootMargin: '0px 0px -50px 0px'
};
const observer = new IntersectionObserver(function(entries) {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('fade-in', 'visible');
}
});
}, observerOptions);
fadeElements.forEach(element => {
element.classList.add('fade-in');
observer.observe(element);
});
}
initScrollAnimations();
// Portfolio items visibility animation
function initPortfolioAnimations() {
const portfolioItems = document.querySelectorAll('.portfolio-item');
const observerOptions = {
threshold: 0.1,
rootMargin: '0px 0px -50px 0px'
};
const observer = new IntersectionObserver(function(entries) {
entries.forEach((entry, index) => {
if (entry.isIntersecting) {
setTimeout(() => {
entry.target.classList.add('visible');
}, index * 200);
observer.unobserve(entry.target);
}
});
}, observerOptions);
portfolioItems.forEach(item => observer.observe(item));
}
initPortfolioAnimations();
// Contact form handling
function initContactForm() {
const contactForm = document.getElementById('contactForm');
if (contactForm) {
contactForm.addEventListener('submit', function(e) {
e.preventDefault();
// Get form data
const formData = new FormData(this);
const name = formData.get('name');
const email = formData.get('email');
const subject = formData.get('subject');
const message = formData.get('message');
// Basic validation
if (!name || !email || !subject || !message) {
showNotification('Please fill in all fields', 'error');
return;
}
// Email validation
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(email)) {
showNotification('Please enter a valid email address', 'error');
return;
}
// Simulate form submission
const submitBtn = this.querySelector('button[type="submit"]');
const originalText = submitBtn.textContent;
submitBtn.textContent = 'Sending...';
submitBtn.disabled = true;
setTimeout(() => {
showNotification('Thank you for your message! I\'ll get back to you soon.', 'success');
contactForm.reset();
submitBtn.textContent = originalText;
submitBtn.disabled = false;
}, 2000);
});
}
}
initContactForm();
// Notification system
function showNotification(message, type = 'info') {
// Remove existing notifications
const existingNotification = document.querySelector('.notification');
if (existingNotification) {
existingNotification.remove();
}
// Create notification element
const notification = document.createElement('div');
notification.className = `notification notification-${type}`;
notification.innerHTML = `
<div class="notification-content">
<span class="notification-message">${message}</span>
<button class="notification-close">×</button>
</div>
`;
// Add styles
notification.style.cssText = `
position: fixed;
top: 20px;
right: 20px;
background: ${type === 'success' ? '#10b981' : type === 'error' ? '#ef4444' : '#3b82f6'};
color: white;
padding: 1rem 1.5rem;
border-radius: 8px;
box-shadow: 0 10px 25px rgba(0,0,0,0.2);
z-index: 10000;
transform: translateX(100%);
transition: transform 0.3s ease;
max-width: 400px;
`;
notification.querySelector('.notification-content').style.cssText = `
display: flex;
align-items: center;
justify-content: space-between;
gap: 1rem;
`;
notification.querySelector('.notification-close').style.cssText = `
background: none;
border: none;
color: white;
font-size: 1.5rem;
cursor: pointer;
padding: 0;
width: 24px;
height: 24px;
display: flex;
align-items: center;
justify-content: center;
`;
document.body.appendChild(notification);
// Animate in
setTimeout(() => {
notification.style.transform = 'translateX(0)';
}, 10);
// Close button functionality
notification.querySelector('.notification-close').addEventListener('click', () => {
notification.style.transform = 'translateX(100%)';
setTimeout(() => notification.remove(), 300);
});
// Auto remove after 5 seconds
setTimeout(() => {
if (notification.parentNode) {
notification.style.transform = 'translateX(100%)';
setTimeout(() => notification.remove(), 300);
}
}, 5000);
}
// Smooth hero button scrolling
const heroButtons = document.querySelectorAll('.hero-buttons .btn');
heroButtons.forEach(btn => {
btn.addEventListener('click', function(e) {
const href = this.getAttribute('href');
if (href && href.startsWith('#')) {
e.preventDefault();
const targetId = href.substring(1);
const targetSection = document.getElementById(targetId);
if (targetSection) {
const offsetTop = targetSection.offsetTop - 70;
window.scrollTo({
top: offsetTop,
behavior: 'smooth'
});
}
}
});
});
// Parallax effect for hero section
function initParallaxEffect() {
const hero = document.querySelector('.hero');
window.addEventListener('scroll', () => {
const scrolled = window.pageYOffset;
const rate = scrolled * -0.5;
if (hero) {
hero.style.transform = `translateY(${rate}px)`;
}
});
}
// Uncomment to enable parallax effect
// initParallaxEffect();
// Intersection Observer for revealing elements
function initRevealAnimation() {
const revealElements = document.querySelectorAll('section');
const observerOptions = {
threshold: 0.15,
rootMargin: '0px 0px -100px 0px'
};
const observer = new IntersectionObserver(function(entries) {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.style.opacity = '1';
entry.target.style.transform = 'translateY(0)';
}
});
}, observerOptions);
revealElements.forEach(element => {
element.style.opacity = '0';
element.style.transform = 'translateY(30px)';
element.style.transition = 'opacity 0.8s ease, transform 0.8s ease';
observer.observe(element);
});
}
// Initialize reveal animations after a short delay
setTimeout(initRevealAnimation, 500);
// Keyboard navigation accessibility
document.addEventListener('keydown', function(e) {
if (e.key === 'Escape') {
// Close mobile menu on escape
navMenu.classList.remove('active');
navToggle.classList.remove('active');
}
});
// Performance optimization: Debounce scroll events
function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
// Apply debounced scroll handler
const debouncedScrollHandler = debounce(() => {
updateActiveNavLink();
}, 10);
window.addEventListener('scroll', debouncedScrollHandler);
// Console welcome message
console.log('%c👋 Welcome to Abc Singh\'s Portfolio!', 'color: #3b82f6; font-size: 16px; font-weight: bold;');
console.log('%cBuilt with vanilla HTML, CSS, and JavaScript', 'color: #10b981; font-size: 12px;');
});
// Additional utility functions
// Function to check if element is in viewport
function isElementInViewport(el) {
const rect = el.getBoundingClientRect();
return (
rect.top >= 0 &&
rect.left >= 0 &&
rect.bottom <= (window.innerHeight || document.documentElement.clientHeight) &&
rect.right <= (window.innerWidth || document.documentElement.clientWidth)
);
}
// Function to animate number counting
function animateNumber(element, start, end, duration) {
const range = end - start;
const minTimer = 50;
let stepTime = Math.abs(Math.floor(duration / range));
stepTime = Math.max(stepTime, minTimer);
const startTime = new Date().getTime();
const endTime = startTime + duration;
function run() {
const now = new Date().getTime();
const remaining = Math.max((endTime - now) / duration, 0);
const value = Math.round(end - (remaining * range));
element.textContent = value;
if (value === end) {
clearInterval(timer);
}
}
const timer = setInterval(run, stepTime);
run();
}
// Function to get random color from theme palette
function getRandomThemeColor() {
const colors = ['#3b82f6', '#2563eb', '#1d4ed8', '#1e40af'];
return colors[Math.floor(Math.random() * colors.length)];
}
// Function to validate email format
function isValidEmail(email) {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}
// Export functions for potential external use
window.portfolioUtils = {
isElementInViewport,
animateNumber,
getRandomThemeColor,
isValidEmail
};