-
Notifications
You must be signed in to change notification settings - Fork 0
/
portfolio.js
429 lines (331 loc) · 14 KB
/
portfolio.js
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
// JavaScript for Admin Login
const loginForm = document.querySelector("#admin-login-form");
const loginBtn = document.querySelector("#admin-login-btn");
const logoutBtn = document.querySelector("#admin-logout-btn");
const adminSection = document.querySelector("#admin-section");
loginBtn.addEventListener("click", function(e) {
e.preventDefault();
const username = loginForm.username.value;
const password = loginForm.password.value;
// check if username and password match admin credentials
if (username === "admin" && password === "password") {
adminSection.style.display = "block";
loginForm.reset();
} else {
alert("Incorrect username or password!");
}
});
logoutBtn.addEventListener("click", function() {
adminSection.style.display = "none";
});
// JavaScript for Skills Bar Animation
const skillsContainer = document.querySelector(".skills-container");
window.addEventListener("scroll", function() {
let skillsContainerCoords = skillsContainer.getBoundingClientRect();
let skillsContainerHeight = skillsContainerCoords.height;
let windowHeight = document.documentElement.clientHeight;
if (skillsContainerCoords.top < windowHeight && skillsContainerCoords.bottom > 0) {
let skillBars = document.querySelectorAll(".skill-bar");
skillBars.forEach(function(skillBar) {
skillBar.style.width = skillBar.dataset.percentage;
});
}
});
// JavaScript for Responsive Navigation
const navigation = document.querySelector("header nav");
const menuBtn = document.querySelector("#menu-btn");
menuBtn.addEventListener("click", function() {
navigation.classList.toggle("active");
});
// JavaScript for Contact Form Validation
const contactForm = document.querySelector("#contact form");
contactForm.addEventListener("submit",function(e) {
e.preventDefault();
let isValid = true;
// check if name field is filled
if (contactForm.name.value === "") {
isValid = false;
alert("Please enter your name!");
}
// check if email field is filled and in valid format
if (contactForm.email.value === "") {
isValid = false;
alert("Please enter your email!");
} else if (!/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(contactForm.email.value)) {
isValid = false;
alert("Please enter a valid email address!");
}
// check if message field is filled
if (contactForm.message.value === "") {
isValid = false;
alert("Please enter a message!");
}
if (isValid) {
alert("Thank you for your message! I will get back to you soon.");
contactForm.reset();
}
});
// JavaScript for List Item Hover Effect
const listItems = document.querySelectorAll("#list li, .inside .list li");
for (let i = 0; i < listItems.length; i++) {
listItems[i].addEventListener("mouseover", function() {
this.style.color = "#ffa600";
});
listItems[i].addEventListener("mouseout", function() {
this.style.color = "#000000";
});
}
// JavaScript for Responsive Layout
const inside = document.querySelector(".inside");
const windowWidth = window.innerWidth;
if (windowWidth <= 768) {
inside.style.flexDirection = "column";
inside.style.alignItems = "center";
} else {
inside.style.flexDirection = "row";
inside.style.alignItems = "flex-start";
}
window.addEventListener("resize", function() {
const windowWidth = window.innerWidth;
if (windowWidth <= 768) {
inside.style.flexDirection = "column";
inside.style.alignItems = "center";
} else {
inside.style.flexDirection = "row";
inside.style.alignItems = "flex-start";
}
});
// JavaScript for Admin Login
const loginForm = document.querySelector("#admin-login-form");
const loginBtn = document.querySelector("#admin-login-btn");
const logoutBtn = document.querySelector("#admin-logout-btn");
const adminSection = document.querySelector("#admin-section");
loginBtn.addEventListener("click", function(e) {
e.preventDefault();
const username = loginForm.username.value;
const password = loginForm.password.value;
// check if username and password match admin credentials
if (username === "admin" && password === "password") {
adminSection.style.display = "block";
loginForm.reset();
} else {
alert("Incorrect username or password!");
}
});
logoutBtn.addEventListener("click", function() {
adminSection.style.display = "none";
});
// JavaScript for Personal Information Edit
const personalInfo = document.querySelector("#personal-info");
const editBtn = document.querySelector("#edit-info-btn");
const saveBtn = document.querySelector("#save-info-btn");
editBtn.addEventListener("click", function() {
personalInfo.contentEditable = "true";
editBtn.style.display = "none";
saveBtn.style.display = "block";
});
saveBtn.addEventListener("click", function() {
personalInfo.contentEditable = "false";
saveBtn.style.display = "none";
editBtn.style.display = "block";
});// JavaScript for Image Gallery
const gallery = document.querySelector(".gallery");
const images = document.querySelectorAll(".gallery img");
const modal = document.querySelector(".modal");
const modalImg = document.querySelector(".modal img");
const closeBtn = document.querySelector(".modal .close-btn");
images.forEach(function(img) {
img.addEventListener("click", function() {
modal.style.display = "block";
modalImg.src = this.src;
});
});
closeBtn.addEventListener("click", function() {
modal.style.display = "none";
});
modal.addEventListener("click", function(e) {
if (e.target === modal) {
modal.style.display = "none";
}
});
// JavaScript for Scroll to Top
const scrollBtn = document.querySelector("#scroll-top-btn");
window.addEventListener("scroll", function() {
if (window.pageYOffset > 100) {
scrollBtn.style.display = "block";
} else {
scrollBtn.style.display = "none";
}
});
scrollBtn.addEventListener("click", function() {
window.scrollTo({
top: 0,
behavior: "smooth"
});
});
// JavaScript for Admin Login
const loginForm = document.querySelector("#admin-login-form");
const loginBtn = document.querySelector("#admin-login-btn");
const logoutBtn = document.querySelector("#admin-logout-btn");
const adminSection = document.querySelector("#admin-section");
loginBtn.addEventListener("click", function(e) {
e.preventDefault();
const username = loginForm.username.value;
const password = loginForm.password.value;
// check if username and password match admin credentials
if (username === "admin" && password === "password") {
adminSection.style.display = "block";
loginForm.reset();
} else {
alert("Incorrect username or password!");
}
});
logoutBtn.addEventListener("click", function() {
adminSection.style.display = "none";
});
// JavaScript for Portfolio Item Filtering
const portfolioItems = document.querySelectorAll(".portfolio-item");
const filterButtons = document.querySelectorAll(".filter-btn");
filterButtons.forEach(function(btn) {
btn.addEventListener("click", function() {
const filter = this.dataset.filter;
portfolioItems.forEach(function(item) {
if (filter === "all") {
item.style.display = "block";
} else {
if (item.classList.contains(filter)) {
item.style.display = "block";
} else {
item.style.display = "none";
}
}
});
});
});
// JavaScript for Modal for Portfolio Item
const portfolioModal = document.querySelector(".portfolio-modal");
const portfolioItems2 = document.querySelectorAll(".portfolio-item");
const closeModalBtn = document.querySelector(".portfolio-modal .close-btn");
portfolioItems2.forEach(function(item) {
item.addEventListener("click", function() {
portfolioModal.style.display = "block";
// insert item information into modal
});
});
closeModalBtn.addEventListener("click", function() {
portfolioModal.style.display = "none";
});
portfolioModal.addEventListener("click", function(e) {
if (e.target === portfolioModal) {
portfolioModal.style.display = "none";
}
});
// JavaScript for Admin Login
const loginForm = document.querySelector("#admin-login-form");
const loginBtn = document.querySelector("#admin-login-btn");
constlogoutBtn = document.querySelector("#admin-logout-btn");
const adminSection = document.querySelector("#admin-section");
loginBtn.addEventListener("click", function(e) {
e.preventDefault();
const username = loginForm.username.value;
const password = loginForm.password.value;
// check if username and password match admin credentials
if (username === "admin" && password === "password") {
adminSection.style.display = "block";
loginForm.reset();
} else {
alert("Incorrect username or password!");
}
});
logoutBtn.addEventListener("click", function() {
adminSection.style.display = "none";
});
// JavaScript for Skills Progress Bar
const skills = document.querySelectorAll(".skill");
skills.forEach(function(skill) {
const skillValue = skill.dataset.value;
const skillBar = skill.querySelector(".skill-bar");
skillBar.style.width = skillValue + "%";
});
// JavaScript for Testimonials Slider
const testimonials = document.querySelectorAll(".testimonial");
const prevBtn = document.querySelector("#prev-btn");
const nextBtn = document.querySelector("#next-btn");
let currentTestimonial = 0;
testimonials[currentTestimonial].style.display = "block";
prevBtn.addEventListener("click", function() {
testimonials[currentTestimonial].style.display = "none";
currentTestimonial--;
if (currentTestimonial < 0) {
currentTestimonial = testimonials.length - 1;
}
testimonials[currentTestimonial].style.display = "block";
});
nextBtn.addEventListener("click", function() {
testimonials[currentTestimonial].style.display = "none";
currentTestimonial++;
if (currentTestimonial === testimonials.length) {
currentTestimonial = 0;
}
testimonials[currentTestimonial].style.display = "block";
});
// JavaScript for Mobile Navigation
const mobileNav = document.querySelector("#mobile-nav");
const navBar = document.querySelector("nav");
const closeNavBtn = document.querySelector("#close-nav-btn");
mobileNav.addEventListener("click", function() {
navBar.style.display = "block";
});
closeNavBtn.addEventListener("click", function() {
navBar.style.display = "none";
});
// JavaScript for Form Validation
const contactForm = document.querySelector("#contact-form");
contactForm.addEventListener("submit", function(e) {
e.preventDefault();
const name = contactForm.name.value;
const email = contactForm.email.value;
const message = contactForm.message.value;
if (name === "" || email === "" || message === "") {
alert("Please fill in all fields!");
} else {
alert("Thank you for your message! I will respond as soon as possible.");
contactForm.reset();
}
});
// JavaScript for Typing Effect
const text = document.querySelector("#typing-text");
const textArray = ["Web Developer", "Designer", "Photographer"];
let textIndex = 0;
let letterIndex = 0;
setInterval(function() {
if (letterIndex === textArray[textIndex].length) {
textIndex++;
letterIndex = 0;
}
if (textIndex === textArray.length) {
textIndex = 0;
}
text.textContent += textArray[textIndex][letterIndex];
letterIndex++;
}, 150);
// JavaScript for Progress Bar
const progressBar = document.querySelector("#progress-bar");
window.addEventListener("scroll", function() {
const winScroll = document.body.scrollTop || document.documentElement.scrollTop;
const height = document.documentElement.scrollHeight - document.documentElement.clientHeight;
const scrolled = (winScroll / height) * 100;
progressBar.style.width = scrolled + "%";
});
// JavaScript for Light/Dark Mode Switch
const switchBtn = document.querySelector("#mode-switch-btn");
const body = document.querySelector("body");
const currentTheme = localStorage.getItem("theme");
if (currentTheme) {
body.classList.add(currentTheme);
if (currentTheme === "light-mode") {
switchBtn.innerHTML = '<i class="fas fa-moon"></i>';
} else {
switchBtn.innerHTML = '<i class="fas fa-sun"></i>';
}
}