-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
630 lines (531 loc) · 21 KB
/
script.js
File metadata and controls
630 lines (531 loc) · 21 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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
// Complete JavaScript for Isuru Eranda Portfolio
// ================================================================
// Author: Isuru Eranda
// Version: 2.0
// Description: Interactive portfolio functionality with mobile menu,
// theme toggle, navigation, and enhanced image loading
// ================================================================
// ============== GLOBAL VARIABLES ==============
let mobileMenuOpen = false;
// ============== MOBILE MENU FUNCTIONS ==============
function openMobileMenu() {
const mobileMenu = document.getElementById('mobile-menu');
const mobileOverlay = document.getElementById('mobile-menu-overlay');
const mobileToggle = document.getElementById('mobile-menu-toggle');
if (mobileMenu && mobileOverlay && mobileToggle) {
mobileMenu.classList.add('active');
mobileOverlay.classList.add('active');
mobileToggle.classList.add('active');
document.body.style.overflow = 'hidden';
mobileMenuOpen = true;
// Force visibility with inline styles as backup
mobileMenu.style.right = '0';
mobileMenu.style.visibility = 'visible';
mobileOverlay.style.opacity = '1';
mobileOverlay.style.visibility = 'visible';
console.log('Mobile menu opened');
}
}
function closeMobileMenu() {
const mobileMenu = document.getElementById('mobile-menu');
const mobileOverlay = document.getElementById('mobile-menu-overlay');
const mobileToggle = document.getElementById('mobile-menu-toggle');
if (mobileMenu && mobileOverlay && mobileToggle) {
mobileMenu.classList.remove('active');
mobileOverlay.classList.remove('active');
mobileToggle.classList.remove('active');
document.body.style.overflow = '';
mobileMenuOpen = false;
// Reset inline styles
setTimeout(() => {
mobileMenu.style.right = '';
mobileMenu.style.visibility = '';
mobileOverlay.style.opacity = '';
mobileOverlay.style.visibility = '';
}, 400); // Wait for transition to complete
console.log('Mobile menu closed');
}
}
// ============== NAVIGATION FUNCTIONS ==============
// Navigation Click Handler
function handleNavClick(e) {
e.preventDefault();
const targetId = this.getAttribute('href');
let target = document.querySelector(targetId);
if (targetId === '#header') {
target = document.querySelector('#intro') || document.querySelector('#header');
}
if (target) {
if (mobileMenuOpen) {
closeMobileMenu();
}
const nav = document.querySelector('nav');
const navHeight = nav ? nav.offsetHeight : 80;
const targetPosition = target.offsetTop - navHeight - 10;
window.scrollTo({
top: Math.max(0, targetPosition),
behavior: 'smooth'
});
// Update active states
document.querySelectorAll('.nav-link').forEach(link => {
link.classList.remove('active');
});
this.classList.add('active');
// Update progress bar
updateNavProgressBar(targetId);
}
}
// ============== NAVIGATION PROGRESS BAR ==============
function updateNavProgressBar(targetId) {
const progressFill = document.getElementById('nav-progress-fill');
let progressPercentage = 0;
// Define progress percentages for each section
switch(targetId) {
case '#header':
progressPercentage = 0;
break;
case '#about':
progressPercentage = 20;
break;
case '#Services':
progressPercentage = 40;
break;
case '#skills':
progressPercentage = 60;
break;
case '#protfolio':
progressPercentage = 80;
break;
case '#Contact':
progressPercentage = 100;
break;
default:
progressPercentage = 0;
}
if (progressFill) {
progressFill.style.width = progressPercentage + '%';
}
}
function updateProgressBarOnScroll() {
const sections = [
{ id: 'header', element: document.querySelector('#intro') || document.querySelector('#header'), progress: 0 },
{ id: 'about', element: document.querySelector('#about'), progress: 20 },
{ id: 'Services', element: document.querySelector('#Services'), progress: 40 },
{ id: 'skills', element: document.querySelector('#skills'), progress: 60 },
{ id: 'protfolio', element: document.querySelector('#protfolio'), progress: 80 },
{ id: 'Contact', element: document.querySelector('#Contact'), progress: 100 }
];
const scrollPosition = window.pageYOffset + window.innerHeight / 2;
const progressFill = document.getElementById('nav-progress-fill');
let currentSection = sections[0];
for (let i = 0; i < sections.length; i++) {
if (sections[i].element && scrollPosition >= sections[i].element.offsetTop) {
currentSection = sections[i];
}
}
if (progressFill) {
progressFill.style.width = currentSection.progress + '%';
}
// Update active nav link
document.querySelectorAll('.nav-link').forEach(link => {
link.classList.remove('active');
if (link.getAttribute('href') === '#' + currentSection.id) {
link.classList.add('active');
}
});
}
function updateProgressBarPosition() {
const nav = document.querySelector('nav');
const progressBar = document.querySelector('.nav-progress-bar');
if (nav && progressBar) {
const navHeight = nav.offsetHeight;
progressBar.style.top = navHeight + 'px';
}
}
// ============== UI COMPONENTS ==============
// Tab System for About Section
function opentab(tabname) {
var tablinks = document.getElementsByClassName("tab-links");
var tabcontents = document.getElementsByClassName("tab-contents");
for (let tablink of tablinks) {
tablink.classList.remove("active-link");
}
for (let tabcontent of tabcontents) {
tabcontent.classList.remove("active-tab");
}
event.currentTarget.classList.add("active-link");
document.getElementById(tabname).classList.add("active-tab");
}
// Portfolio Filter
function filterPortfolio(category) {
const items = document.querySelectorAll('.work');
const filterBtns = document.querySelectorAll('.filter-btn');
// Remove active class from all buttons
filterBtns.forEach(btn => btn.classList.remove('active'));
// Add active class to clicked button
event.currentTarget.classList.add('active');
// Filter items
items.forEach(item => {
if (category === 'all' || item.dataset.category === category) {
item.style.display = 'block';
item.style.animation = 'fadeInUp 0.5s ease';
} else {
item.style.display = 'none';
}
});
}
// Theme Toggle
function toggleTheme() {
const body = document.body;
const themeToggle = document.getElementById('theme-toggle');
const icon = themeToggle.querySelector('i');
body.classList.toggle('dark-mode');
if (body.classList.contains('dark-mode')) {
icon.className = 'fa-solid fa-sun';
localStorage.setItem('darkMode', 'enabled');
} else {
icon.className = 'fa-solid fa-moon';
localStorage.setItem('darkMode', 'disabled');
}
}
// Skills Animation
function animateSkills() {
const skillBars = document.querySelectorAll('.progress-bar');
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const progressBar = entry.target;
const percentage = progressBar.dataset.percentage;
progressBar.style.width = percentage + '%';
}
});
}, { threshold: 0.5 });
skillBars.forEach(bar => observer.observe(bar));
}
// Scroll to Top
function scrollToTop() {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
}
// Loading Screen
function hideLoadingScreen() {
const loadingScreen = document.getElementById('loading-screen');
if (loadingScreen) {
loadingScreen.style.opacity = '0';
setTimeout(() => {
loadingScreen.style.display = 'none';
}, 500);
}
}
// Contact Form
const scriptURL = 'https://script.google.com/macros/s/AKfycbzBOi-dDtOPLcaK-t0h5X-jdCJyj1M5PKFdOmhR0cJ7SgWZ29NhQdC_fE8q3zTFJzPd/exec';
function submitForm() {
const form = document.forms['submit-to-google-sheet'];
const msg = document.getElementById("msg");
fetch(scriptURL, { method: 'POST', body: new FormData(form) })
.then(response => {
msg.innerHTML = "Message sent successfully!";
msg.style.color = "#28a745";
setTimeout(() => {
msg.innerHTML = "";
}, 5000);
form.reset();
})
.catch(error => {
msg.innerHTML = "Error sending message. Please try again.";
msg.style.color = "#dc3545";
console.error('Error!', error.message);
});
}
// Enhanced Image Loading for Skills
function initializeSkillImages() {
const skillImages = document.querySelectorAll('.skill-icon img');
skillImages.forEach(img => {
// Add loading event
img.addEventListener('load', function() {
this.style.opacity = '1';
this.style.transform = 'scale(1)';
console.log(`✅ Loaded: ${this.alt}`);
});
// Add error handling with multiple fallback options
img.addEventListener('error', function() {
console.log(`❌ Failed to load: ${this.alt}, trying fallback...`);
// First try the onerror fallback (CDN)
if (this.src.includes('Images/skills/')) {
const cdnUrl = this.getAttribute('onerror').match(/this\.src='([^']+)'/);
if (cdnUrl && cdnUrl[1]) {
this.src = cdnUrl[1];
console.log(`🔄 Trying CDN for ${this.alt}: ${cdnUrl[1]}`);
return;
}
}
// If CDN also fails, create a styled fallback
const fallbackIcon = document.createElement('div');
fallbackIcon.style.cssText = `
width: 40px;
height: 40px;
background: var(--gradient-primary);
border-radius: 8px;
display: flex;
align-items: center;
justify-content: center;
color: white;
font-weight: bold;
font-size: 14px;
text-transform: uppercase;
letter-spacing: 1px;
box-shadow: 0 2px 8px rgba(220, 38, 38, 0.3);
`;
// Get first 2-3 characters of skill name for fallback
const skillName = this.alt;
let fallbackText = '';
if (skillName.includes('.')) {
fallbackText = skillName.split('.')[0].substring(0, 2);
} else if (skillName.includes(' ')) {
const words = skillName.split(' ');
fallbackText = words.map(word => word.charAt(0)).join('').substring(0, 3);
} else {
fallbackText = skillName.substring(0, 3);
}
fallbackIcon.textContent = fallbackText;
fallbackIcon.title = `${skillName} (Image not available)`;
// Replace the broken image
this.parentNode.replaceChild(fallbackIcon, this);
console.log(`🔧 Created fallback for ${skillName}: ${fallbackText}`);
});
// Set initial state for smooth loading animation
img.style.opacity = '0';
img.style.transform = 'scale(0.8)';
img.style.transition = 'all 0.3s ease';
// Force image loading
if (img.complete && img.naturalWidth > 0) {
img.style.opacity = '1';
img.style.transform = 'scale(1)';
}
});
// Add a global image error handler as backup
window.addEventListener('error', function(e) {
if (e.target.tagName === 'IMG' && e.target.closest('.skill-icon')) {
console.log('Global error handler triggered for skill image:', e.target.alt);
}
}, true);
}
// Initialize Everything
document.addEventListener('DOMContentLoaded', function() {
console.log('Initializing portfolio...');
// Initialize theme
if (localStorage.getItem('darkMode') === 'enabled') {
document.body.classList.add('dark-mode');
const themeToggle = document.getElementById('theme-toggle');
if (themeToggle) {
themeToggle.querySelector('i').className = 'fa-solid fa-sun';
}
}
// Mobile Menu Toggle
const mobileToggle = document.getElementById('mobile-menu-toggle');
if (mobileToggle) {
console.log('Mobile toggle button found');
mobileToggle.addEventListener('click', function(e) {
e.preventDefault();
e.stopPropagation();
console.log('Mobile toggle clicked, current state:', mobileMenuOpen);
if (mobileMenuOpen) {
closeMobileMenu();
} else {
openMobileMenu();
}
});
} else {
console.warn('Mobile toggle button not found');
}
// Mobile Close Button
const mobileCloseBtn = document.getElementById('mobile-close-btn');
if (mobileCloseBtn) {
console.log('Mobile close button found');
mobileCloseBtn.addEventListener('click', function(e) {
e.preventDefault();
closeMobileMenu();
});
} else {
console.warn('Mobile close button not found');
}
// Mobile Overlay
const mobileOverlay = document.getElementById('mobile-menu-overlay');
if (mobileOverlay) {
console.log('Mobile overlay found');
mobileOverlay.addEventListener('click', closeMobileMenu);
} else {
console.warn('Mobile overlay not found');
}
// Navigation Links (both desktop and mobile)
document.querySelectorAll('.nav-link').forEach(link => {
link.addEventListener('click', handleNavClick);
});
// Mobile Navigation Links
document.querySelectorAll('.mobile-nav-link').forEach(link => {
link.addEventListener('click', function(e) {
e.preventDefault();
const targetId = this.getAttribute('href');
let target = document.querySelector(targetId);
if (targetId === '#header') {
target = document.querySelector('#intro') || document.querySelector('#header');
}
if (target) {
// Close mobile menu first
closeMobileMenu();
// Wait for menu to close, then scroll
setTimeout(() => {
const nav = document.querySelector('nav');
const navHeight = nav ? nav.offsetHeight : 80;
const targetPosition = target.offsetTop - navHeight - 10;
window.scrollTo({
top: Math.max(0, targetPosition),
behavior: 'smooth'
});
// Update active states for both desktop and mobile
document.querySelectorAll('.nav-link, .mobile-nav-link').forEach(navLink => {
navLink.classList.remove('active');
});
this.classList.add('active');
// Also update desktop nav if same href exists
const desktopLink = document.querySelector(`.nav-menu a[href="${targetId}"]`);
if (desktopLink) {
desktopLink.classList.add('active');
}
}, 200);
}
});
});
// Theme Toggle
const themeToggle = document.getElementById('theme-toggle');
if (themeToggle) {
themeToggle.addEventListener('click', toggleTheme);
}
// Tab System
document.querySelectorAll('.tab-links').forEach(tab => {
tab.addEventListener('click', function() {
opentab(this.dataset.tab);
});
});
// Portfolio Filter
document.querySelectorAll('.filter-btn').forEach(btn => {
btn.addEventListener('click', function() {
filterPortfolio(this.dataset.filter);
});
});
// Contact Form
const contactForm = document.forms['submit-to-google-sheet'];
if (contactForm) {
contactForm.addEventListener('submit', function(e) {
e.preventDefault();
submitForm();
});
}
// Scroll to Top Button
const scrollTopBtn = document.getElementById('scroll-top');
if (scrollTopBtn) {
scrollTopBtn.addEventListener('click', scrollToTop);
window.addEventListener('scroll', function() {
if (window.pageYOffset > 300) {
scrollTopBtn.classList.add('show');
} else {
scrollTopBtn.classList.remove('show');
}
// Update progress bar based on scroll position
updateProgressBarOnScroll();
// Update progress bar position based on nav height
updateProgressBarPosition();
});
}
// Escape key for mobile menu
document.addEventListener('keydown', function(e) {
if (e.key === 'Escape' && mobileMenuOpen) {
closeMobileMenu();
}
});
// Close mobile menu on resize
window.addEventListener('resize', function() {
if (window.innerWidth > 768 && mobileMenuOpen) {
closeMobileMenu();
}
});
// Initialize skills animation
animateSkills();
// Initialize skill images
initializeSkillImages();
// Debug skill images after a delay
setTimeout(() => {
debugSkillImages();
}, 2000);
// Set initial active link
const homeLinks = document.querySelectorAll('.nav-link[href="#header"]');
homeLinks.forEach(link => {
link.classList.add('active');
});
// Set initial progress bar position
updateProgressBarPosition();
console.log('Portfolio initialized successfully');
});
// Hide loading screen when page loads
window.addEventListener('load', function() {
hideLoadingScreen();
console.log('Page loaded completely');
});
// Typing Animation for Hero Section
document.addEventListener('DOMContentLoaded', function() {
const typingText = document.querySelector('.typing-text');
if (typingText) {
const texts = [
'Experienced Front-End Developer',
'UI/UX Designer',
'Creative Problem Solver',
'Full-Stack Developer'
];
let textIndex = 0;
let charIndex = 0;
let isDeleting = false;
function typeText() {
const currentText = texts[textIndex];
if (isDeleting) {
typingText.textContent = currentText.substring(0, charIndex - 1);
charIndex--;
} else {
typingText.textContent = currentText.substring(0, charIndex + 1);
charIndex++;
}
let typeSpeed = isDeleting ? 100 : 150;
if (!isDeleting && charIndex === currentText.length) {
typeSpeed = 2000;
isDeleting = true;
} else if (isDeleting && charIndex === 0) {
isDeleting = false;
textIndex = (textIndex + 1) % texts.length;
typeSpeed = 500;
}
setTimeout(typeText, typeSpeed);
}
typeText();
}
});
// Debug function to check skill image loading
function debugSkillImages() {
console.log('🔍 SKILL IMAGES DEBUG REPORT');
console.log('================================');
const skillImages = document.querySelectorAll('.skill-icon img');
const fallbackElements = document.querySelectorAll('.skill-icon div[title*="Image not available"]');
console.log(`📊 Total skill images: ${skillImages.length}`);
console.log(`🔧 Fallback elements: ${fallbackElements.length}`);
skillImages.forEach((img, index) => {
const status = img.complete && img.naturalWidth > 0 ? '✅' : '❌';
console.log(`${status} ${img.alt}: ${img.src}`);
if (!img.complete || img.naturalWidth === 0) {
console.log(` └─ Failed to load, naturalWidth: ${img.naturalWidth}, complete: ${img.complete}`);
}
});
fallbackElements.forEach((element, index) => {
console.log(`🔧 Fallback ${index + 1}: ${element.title}`);
});
console.log('================================');
}