-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
143 lines (125 loc) · 4.52 KB
/
script.js
File metadata and controls
143 lines (125 loc) · 4.52 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
console.log('Navigation menu toggled');
// Toggle navigation menu
function toggleMenu() {
const menu = document.getElementById('nav-menu');
menu.classList.toggle('active');
}
// Add event listener to the menu icon
const menuIcon = document.getElementById('menu-icon');
if (menuIcon) {
menuIcon.addEventListener('click', toggleMenu);
}
// Add smooth scrolling to all anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const targetId = this.getAttribute('href').substring(1);
const targetElement = document.getElementById(targetId);
if (targetElement) {
targetElement.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}
});
});
function filterProjects(category) {
const projects = document.querySelectorAll('.project');
projects.forEach(project => {
if (category === 'all' || project.classList.contains(category)) {
project.style.display = 'block';
} else {
project.style.display = 'none';
}
});
}
// Lightbox effect for .jpg images
let lightbox = null;
let lightboxImg = null;
function openLightbox(imageSrc) {
if (!lightbox) {
lightbox = document.createElement('div');
lightbox.id = 'lightbox';
lightbox.style.position = 'fixed';
lightbox.style.top = '0';
lightbox.style.left = '0';
lightbox.style.width = '100%';
lightbox.style.height = '100%';
lightbox.style.backgroundColor = 'rgba(0, 0, 0, 0.8)';
lightbox.style.display = 'flex';
lightbox.style.justifyContent = 'center';
lightbox.style.alignItems = 'center';
lightbox.style.zIndex = '1000';
lightboxImg = document.createElement('img');
lightboxImg.style.maxWidth = '90%';
lightboxImg.style.maxHeight = '90%';
lightboxImg.classList.add('lightbox-img');
lightbox.addEventListener('click', (e) => {
if (e.target === lightbox) {
lightbox.style.display = 'none';
}
});
lightbox.appendChild(lightboxImg);
document.body.appendChild(lightbox);
}
lightboxImg.src = imageSrc;
lightbox.style.display = 'flex';
}
// Use event delegation for .jpg images
document.body.addEventListener('click', (event) => {
if (event.target.tagName === 'IMG' && event.target.src.toLowerCase().endsWith('.jpg')) {
openLightbox(event.target.src);
}
});
// Real-time validation for the contact form
const contactForm = document.getElementById('contact-form');
if (contactForm) {
const nameInput = document.getElementById('name');
const emailInput = document.getElementById('email');
const messageInput = document.getElementById('message');
function validateName() {
const nameError = document.getElementById('name-error');
if (nameInput.value.trim() === '') {
nameError.textContent = 'Name is required.';
return false;
} else {
nameError.textContent = '';
return true;
}
}
function validateEmail() {
const emailError = document.getElementById('email-error');
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(emailInput.value.trim())) {
emailError.textContent = 'Please enter a valid email address.';
return false;
} else {
emailError.textContent = '';
return true;
}
}
function validateMessage() {
const messageError = document.getElementById('message-error');
if (messageInput.value.trim() === '') {
messageError.textContent = 'Message cannot be empty.';
return false;
} else {
messageError.textContent = '';
return true;
}
}
nameInput.addEventListener('input', validateName);
emailInput.addEventListener('input', validateEmail);
messageInput.addEventListener('input', validateMessage);
nameInput.addEventListener('blur', validateName);
emailInput.addEventListener('blur', validateEmail);
messageInput.addEventListener('blur', validateMessage);
contactForm.addEventListener('submit', (e) => {
const isNameValid = validateName();
const isEmailValid = validateEmail();
const isMessageValid = validateMessage();
if (!isNameValid || !isEmailValid || !isMessageValid) {
e.preventDefault();
}
});
}