forked from Gyanthakur/Dev_Profiles
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
171 lines (149 loc) · 5.41 KB
/
script.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
document.addEventListener("DOMContentLoaded", function () {
const searchInput = document.getElementById("searchInput");
const profiles = document.querySelectorAll(".profile");
const date = document.getElementById("dates");
let currentDate = new Date();
let currentYear = currentDate.getFullYear();
date.innerHTML = currentYear;
window.addEventListener("scroll", () => {
const progressBar = document.getElementById("bar");
const scrollTotal = document.body.scrollHeight - window.innerHeight;
const scrollPosition = window.scrollY;
const scrollPercentage = (scrollPosition / scrollTotal) * 100;
progressBar.style.width = scrollPercentage + "%";
});
if (searchInput) {
searchInput.addEventListener("input", filterProfiles);
}
function filterProfiles() {
const query = searchInput.value.toLowerCase();
profiles.forEach((profile) => {
const name = profile.querySelector(".name").textContent.toLowerCase();
const skills = Array.from(profile.querySelectorAll(".skills .skill")).map(
(skill) => skill.textContent.toLowerCase()
);
if (
name.includes(query) ||
skills.some((skill) => skill.includes(query))
) {
profile.style.display = "block";
} else {
profile.style.display = "none";
}
});
}
const darkMode = document.querySelector(".dark-btn");
var icon = document.getElementById("icon");
darkMode.addEventListener("click", () => {
document.body.classList.toggle("light-background");
if (document.body.classList.contains("light-background")) {
icon.style.filter = "invert(0%)";
icon.src = "light.png";
} else {
icon.style.filter = "invert(100%)";
icon.src = "dark.png";
}
darkMode.classList.toggle("icon-position");
darkMode.classList.toggle("light-background");
darkMode.classList.toggle("icon-color");
document.querySelector(".title").classList.toggle("text-color");
document.querySelector("footer").classList.toggle("text-color");
const buttons = document.querySelectorAll(".add");
// const contactContainer = document.querySelector(".contact-container");
// if (contactContainer) {
// darkMode.classList.toggle("light-background");
// }
buttons.forEach((button) => {
button.classList.toggle("bg-color");
});
});
});
// Get the button
let backToTopBtn = document.getElementById("backToTopBtn");
// Show the button when the user scrolls down 20px from the top of the document
window.onscroll = function () {
scrollFunction();
};
function scrollFunction() {
if (document.body.scrollTop > 20 || document.documentElement.scrollTop > 20) {
backToTopBtn.style.display = "block";
} else {
backToTopBtn.style.display = "none";
}
}
// When the user clicks the button, scroll to the top of the document
backToTopBtn.onclick = function () {
window.scrollTo({ top: 0, behavior: "smooth" });
};
document
.querySelector(".contact-form")
.addEventListener("submit", function (event) {
event.preventDefault(); // Prevent the form from submitting
// Clear any existing error messages
const errorMessageDiv = document.getElementById("error-message");
const form = event.target;
errorMessageDiv.style.display = "none";
errorMessageDiv.textContent = "";
// Form inputs
const name = document.getElementById("name").value.trim();
const email = document.getElementById("email").value.trim();
const message = document.getElementById("message").value.trim();
// Validate input fields
if (!name || !email || !message) {
showError("All fields are required.");
return;
}
if (!validateEmail(email)) {
showError("Please enter a valid email address.");
return;
}
// Create a form data object to send
const formData = new FormData(form);
// Submit the form using fetch
fetch(form.action, {
method: "POST",
body: formData,
headers: {
Accept: "application/json",
},
})
.then((response) => {
if (response.ok) {
showSuccess("Message sent successfully!");
form.reset(); // Reset the form fields after success
} else {
showError("Oops! Something went wrong. Please try again later.");
}
})
.catch((error) => {
showError("There was a problem submitting the form. Please try again.");
});
});
// Function to show error message
function showError(message) {
const errorMessageDiv = document.getElementById("error-message");
errorMessageDiv.textContent = message;
errorMessageDiv.style.display = "block";
// Hide the error message after 5 seconds
setTimeout(() => {
errorMessageDiv.style.display = "none";
}, 5000);
}
// Function to show success message
function showSuccess(message) {
const errorMessageDiv = document.getElementById("error-message");
errorMessageDiv.textContent = message;
errorMessageDiv.style.backgroundColor = "#4BB543"; // Change background to green for success
errorMessageDiv.style.display = "block";
errorMessageDiv.style.fontWeight = "bolder";
// Hide the success message after 5 seconds
setTimeout(() => {
errorMessageDiv.style.display = "none";
errorMessageDiv.style.backgroundColor = "#ff4d4d"; // Reset to default error color
}, 5000);
}
// Function to validate email format
function validateEmail(email) {
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailPattern.test(email);
}