-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
169 lines (143 loc) · 5.75 KB
/
script.js
File metadata and controls
169 lines (143 loc) · 5.75 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
/* CustomerHub – main script */
(function () {
'use strict';
// ── Footer year ────────────────────────────────────────────────────────────
var yearEl = document.getElementById('year');
if (yearEl) {
yearEl.textContent = new Date().getFullYear();
}
// ── Mobile nav toggle ──────────────────────────────────────────────────────
var menuToggle = document.getElementById('menuToggle');
var nav = document.getElementById('nav');
if (menuToggle && nav) {
menuToggle.addEventListener('click', function () {
var isOpen = nav.classList.toggle('open');
menuToggle.setAttribute('aria-expanded', isOpen ? 'true' : 'false');
});
// Close nav when a link is clicked
nav.querySelectorAll('.nav-link').forEach(function (link) {
link.addEventListener('click', function () {
nav.classList.remove('open');
menuToggle.setAttribute('aria-expanded', 'false');
});
});
}
// ── Active nav link on scroll ──────────────────────────────────────────────
var sections = document.querySelectorAll('main section[id]');
var navLinks = document.querySelectorAll('.nav-link');
function setActiveLink() {
var scrollY = window.scrollY;
sections.forEach(function (section) {
var top = section.offsetTop - 80;
var bottom = top + section.offsetHeight;
if (scrollY >= top && scrollY < bottom) {
navLinks.forEach(function (link) {
link.classList.toggle('active', link.getAttribute('href') === '#' + section.id);
});
}
});
}
window.addEventListener('scroll', setActiveLink, { passive: true });
// ── Animate cards on scroll (IntersectionObserver) ────────────────────────
var animateItems = document.querySelectorAll('[data-animate]');
if ('IntersectionObserver' in window && animateItems.length) {
var observer = new IntersectionObserver(function (entries) {
entries.forEach(function (entry) {
if (entry.isIntersecting) {
entry.target.classList.add('visible');
observer.unobserve(entry.target);
}
});
}, { threshold: 0.15 });
animateItems.forEach(function (el) {
observer.observe(el);
});
} else {
// Fallback: just show all items
animateItems.forEach(function (el) {
el.classList.add('visible');
});
}
// ── Animated stat counters ─────────────────────────────────────────────────
var statNumbers = document.querySelectorAll('.stat-number[data-target]');
function animateCounter(el) {
var target = parseInt(el.getAttribute('data-target'), 10);
var duration = 1500;
var start = null;
function step(timestamp) {
if (!start) start = timestamp;
var progress = Math.min((timestamp - start) / duration, 1);
// Ease-out cubic
var eased = 1 - Math.pow(1 - progress, 3);
el.textContent = Math.floor(eased * target).toLocaleString();
if (progress < 1) {
requestAnimationFrame(step);
} else {
el.textContent = target.toLocaleString();
}
}
requestAnimationFrame(step);
}
if ('IntersectionObserver' in window && statNumbers.length) {
var statObserver = new IntersectionObserver(function (entries) {
entries.forEach(function (entry) {
if (entry.isIntersecting) {
animateCounter(entry.target);
statObserver.unobserve(entry.target);
}
});
}, { threshold: 0.5 });
statNumbers.forEach(function (el) {
statObserver.observe(el);
});
}
// ── Contact form validation ────────────────────────────────────────────────
var form = document.getElementById('contactForm');
var formSuccess = document.getElementById('formSuccess');
function setError(fieldId, errorId, message) {
var field = document.getElementById(fieldId);
var errorEl = document.getElementById(errorId);
if (field) field.classList.toggle('invalid', !!message);
if (errorEl) errorEl.textContent = message || '';
}
function validateEmail(value) {
return /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value);
}
if (form) {
form.addEventListener('submit', function (e) {
e.preventDefault();
var name = document.getElementById('name').value.trim();
var email = document.getElementById('email').value.trim();
var message = document.getElementById('message').value.trim();
var valid = true;
if (!name) {
setError('name', 'nameError', 'Please enter your name.');
valid = false;
} else {
setError('name', 'nameError', '');
}
if (!email) {
setError('email', 'emailError', 'Please enter your email address.');
valid = false;
} else if (!validateEmail(email)) {
setError('email', 'emailError', 'Please enter a valid email address.');
valid = false;
} else {
setError('email', 'emailError', '');
}
if (!message) {
setError('message', 'messageError', 'Please enter a message.');
valid = false;
} else {
setError('message', 'messageError', '');
}
if (valid && formSuccess) {
formSuccess.hidden = false;
form.reset();
setTimeout(function () {
formSuccess.hidden = true;
}, 6000);
}
});
}
})();