-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
96 lines (79 loc) · 3.26 KB
/
Copy pathscript.js
File metadata and controls
96 lines (79 loc) · 3.26 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
// JavaScript functionality for TransportPro Hauliers website
document.addEventListener('DOMContentLoaded', function() {
// Add smooth scrolling for anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function(e) {
e.preventDefault();
const targetId = this.getAttribute('href');
const targetElement = document.querySelector(targetId);
if (targetElement) {
window.scrollTo({
top: targetElement.offsetTop - 70, // Offset for the fixed header
behavior: 'smooth'
});
}
});
});
// Add scroll event to handle header styling
window.addEventListener('scroll', function() {
const header = document.querySelector('header');
if (window.scrollY > 50) {
header.classList.add('scrolled');
} else {
header.classList.remove('scrolled');
}
});
// Form validation and submission handling
const quoteForm = document.getElementById('quote-form');
if (quoteForm) {
quoteForm.addEventListener('submit', function(e) {
e.preventDefault();
// Basic form validation
const name = document.getElementById('name').value.trim();
const email = document.getElementById('email').value.trim();
const message = document.getElementById('message').value.trim();
let isValid = true;
// Very simple validation
if (name === '') {
isValid = false;
highlightField('name');
}
if (email === '' || !isValidEmail(email)) {
isValid = false;
highlightField('email');
}
if (message === '') {
isValid = false;
highlightField('message');
}
if (isValid) {
// In a real application, you would send this data to a server
// For this example, we'll just show a success message
showFormSuccess();
}
});
}
// Helper functions for form validation
function isValidEmail(email) {
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return emailRegex.test(email);
}
function highlightField(fieldId) {
const field = document.getElementById(fieldId);
field.style.borderColor = '#ff3b30';
field.addEventListener('input', function() {
this.style.borderColor = '#ddd';
}, { once: true });
}
function showFormSuccess() {
const form = document.getElementById('quote-form');
const successMessage = document.createElement('div');
successMessage.className = 'success-message';
successMessage.innerHTML = '<p>Thank you for your inquiry! We will get back to you shortly.</p>';
// Replace form with success message
form.style.display = 'none';
form.parentNode.appendChild(successMessage);
// Reset form fields (in case the form is shown again)
form.reset();
}
});