-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
371 lines (320 loc) · 10.4 KB
/
script.js
File metadata and controls
371 lines (320 loc) · 10.4 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
/* jshint esversion: 8 */
// Mobile Navigation Toggle
const navToggle = document.getElementById("nav-toggle");
const navMenu = document.getElementById("nav-menu");
navToggle.addEventListener("click", () => {
navMenu.classList.toggle("active");
navToggle.classList.toggle("active");
});
// Close mobile menu when clicking on links
document.querySelectorAll(".nav-link").forEach((link) => {
link.addEventListener("click", () => {
navMenu.classList.remove("active");
navToggle.classList.remove("active");
});
});
// Smooth scrolling for anchor links
document.querySelectorAll('a[href^="#"]').forEach((anchor) => {
anchor.addEventListener("click", function (e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute("href"));
if (target) {
const offsetTop = target.offsetTop - 80; // Account for fixed navbar
window.scrollTo({
top: offsetTop,
behavior: "smooth",
});
}
});
});
// Navbar background change on scroll
window.addEventListener("scroll", () => {
const navbar = document.querySelector(".navbar");
if (window.scrollY > 50) {
navbar.style.background = "rgba(255, 255, 255, 0.98)";
navbar.style.boxShadow = "0 2px 20px rgba(0, 0, 0, 0.1)";
} else {
navbar.style.background = "rgba(255, 255, 255, 0.95)";
navbar.style.boxShadow = "none";
}
});
// Form handling
const contactForm = document.getElementById("contact-form");
contactForm.addEventListener("submit", async function (e) {
e.preventDefault();
const formData = new FormData(this);
const formObject = {};
// Convert FormData to object
for (let [key, value] of formData.entries()) {
if (formObject[key]) {
// Handle multiple values (like checkboxes)
if (Array.isArray(formObject[key])) {
formObject[key].push(value);
} else {
formObject[key] = [formObject[key], value];
}
} else {
formObject[key] = value;
}
}
// Show loading state
const submitBtn = this.querySelector('button[type="submit"]');
const originalText = submitBtn.innerHTML;
submitBtn.classList.add("loading");
submitBtn.innerHTML = "<span>Sending...</span>";
try {
// Simulate form submission (replace with actual endpoint)
await simulateFormSubmission(formObject);
// Show success message
showSuccessMessage();
// Reset form
this.reset();
} catch (error) {
console.error("Form submission error:", error);
showErrorMessage();
} finally {
// Remove loading state
submitBtn.classList.remove("loading");
submitBtn.innerHTML = originalText;
}
});
// Simulate form submission (replace with actual API call)
function simulateFormSubmission(data) {
return new Promise((resolve, reject) => {
// Log form data for demonstration
console.log("Form submitted with data:", data);
// Simulate network delay
setTimeout(() => {
// Randomly succeed or fail for demo purposes
if (Math.random() > 0.1) {
resolve(data);
} else {
reject(new Error("Submission failed"));
}
}, 2000);
});
}
// Show success message
function showSuccessMessage() {
// Remove any existing messages
const existingMessage = document.querySelector(".form-success");
if (existingMessage) {
existingMessage.remove();
}
// Create success message
const successMessage = document.createElement("div");
successMessage.className = "form-success show";
successMessage.innerHTML = `
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<polyline points="20,6 9,17 4,12"/>
</svg>
<span>Thank you! Your message has been sent successfully. We'll contact you within 2 hours.</span>
`;
// Insert before form
const form = document.getElementById("contact-form");
form.parentNode.insertBefore(successMessage, form);
// Auto-hide after 5 seconds
setTimeout(() => {
successMessage.classList.remove("show");
setTimeout(() => successMessage.remove(), 300);
}, 5000);
// Scroll to success message
successMessage.scrollIntoView({ behavior: "smooth", block: "center" });
}
// Show error message
function showErrorMessage() {
// Remove any existing messages
const existingMessage = document.querySelector(".form-error");
if (existingMessage) {
existingMessage.remove();
}
// Create error message
const errorMessage = document.createElement("div");
errorMessage.className = "form-error show";
errorMessage.style.cssText = `
background: #ef4444;
color: white;
padding: 16px 24px;
border-radius: 12px;
margin-bottom: 24px;
display: flex;
align-items: center;
gap: 12px;
`;
errorMessage.innerHTML = `
<svg width="20" height="20" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
<circle cx="12" cy="12" r="10"/>
<line x1="15" y1="9" x2="9" y2="15"/>
<line x1="9" y1="9" x2="15" y2="15"/>
</svg>
<span>Sorry, there was an error sending your message. Please try again or call us directly.</span>
`;
// Insert before form
const form = document.getElementById("contact-form");
form.parentNode.insertBefore(errorMessage, form);
// Auto-hide after 5 seconds
setTimeout(() => {
errorMessage.remove();
}, 5000);
// Scroll to error message
errorMessage.scrollIntoView({ behavior: "smooth", block: "center" });
}
// Form validation enhancements
function setupFormValidation() {
const form = document.getElementById("contact-form");
const inputs = form.querySelectorAll(
"input[required], select[required], textarea[required]"
);
inputs.forEach((input) => {
input.addEventListener("blur", validateField);
input.addEventListener("input", clearFieldError);
});
}
function validateField(e) {
const field = e.target;
const value = field.value.trim();
// Remove existing error styling
clearFieldError({ target: field });
if (!value) {
showFieldError(field, "This field is required");
return false;
}
// Email validation
if (field.type === "email") {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(value)) {
showFieldError(field, "Please enter a valid email address");
return false;
}
}
// Phone validation
if (field.type === "tel") {
const phoneRegex = /^[\+]?[\d\s\-\(\)]{10,}$/;
if (!phoneRegex.test(value)) {
showFieldError(field, "Please enter a valid phone number");
return false;
}
}
return true;
}
function showFieldError(field, message) {
field.style.borderColor = "#ef4444";
// Remove existing error message
const existingError = field.parentNode.querySelector(".field-error");
if (existingError) {
existingError.remove();
}
// Add error message
const errorDiv = document.createElement("div");
errorDiv.className = "field-error";
errorDiv.style.cssText =
"color: #ef4444; font-size: 0.875rem; margin-top: 4px;";
errorDiv.textContent = message;
field.parentNode.appendChild(errorDiv);
}
function clearFieldError(e) {
const field = e.target;
field.style.borderColor = "#e5e7eb";
const errorDiv = field.parentNode.querySelector(".field-error");
if (errorDiv) {
errorDiv.remove();
}
}
// Intersection Observer for animations
function setupScrollAnimations() {
const observerOptions = {
threshold: 0.1,
rootMargin: "0px 0px -50px 0px",
};
const observer = new IntersectionObserver((entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
entry.target.style.opacity = "1";
entry.target.style.transform = "translateY(0)";
}
});
}, observerOptions);
// Observe service cards
document.querySelectorAll(".service-card").forEach((card, index) => {
card.style.opacity = "0";
card.style.transform = "translateY(30px)";
card.style.transition = `opacity 0.6s ease ${
index * 0.1
}s, transform 0.6s ease ${index * 0.1}s`;
observer.observe(card);
});
// Observe stat items
document.querySelectorAll(".stat-item").forEach((item, index) => {
item.style.opacity = "0";
item.style.transform = "translateY(20px)";
item.style.transition = `opacity 0.5s ease ${
index * 0.2
}s, transform 0.5s ease ${index * 0.2}s`;
observer.observe(item);
});
}
// Counter animation for stats
function animateCounters() {
const counters = document.querySelectorAll(".stat-number");
const observer = new IntersectionObserver(
(entries) => {
entries.forEach((entry) => {
if (entry.isIntersecting) {
const counter = entry.target;
const target = parseInt(counter.textContent.replace(/\D/g, ""));
const suffix = counter.textContent.replace(/\d/g, "");
let current = 0;
const increment = target / 50;
const timer = setInterval(() => {
current += increment;
if (current >= target) {
current = target;
clearInterval(timer);
}
counter.textContent = Math.floor(current) + suffix;
}, 40);
observer.unobserve(counter);
}
});
},
{ threshold: 0.5 }
);
counters.forEach((counter) => observer.observe(counter));
}
// Initialize everything when DOM is loaded
document.addEventListener("DOMContentLoaded", function () {
setupFormValidation();
setupScrollAnimations();
animateCounters();
// Add smooth reveal animation to hero content
const heroContent = document.querySelector(".hero-content");
if (heroContent) {
heroContent.style.opacity = "0";
heroContent.style.transform = "translateY(30px)";
setTimeout(() => {
heroContent.style.transition = "opacity 0.8s ease, transform 0.8s ease";
heroContent.style.opacity = "1";
heroContent.style.transform = "translateY(0)";
}, 200);
}
});
// Handle service selection from hero buttons
document.addEventListener("click", function (e) {
if (e.target.closest('a[href="#contact"]')) {
setTimeout(() => {
const serviceSelect = document.getElementById("service");
if (serviceSelect) {
serviceSelect.focus();
}
}, 1000);
}
});
// Auto-update copyright year
const currentYear = new Date().getFullYear();
const copyrightElement = document.querySelector(".footer-bottom p");
if (copyrightElement) {
copyrightElement.textContent = copyrightElement.textContent.replace(
"2025",
currentYear
);
}