Spaces:
Running
Running
File size: 10,549 Bytes
8309ac6 | 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 | // Main JavaScript for Conversion Catalyst Pro
document.addEventListener('DOMContentLoaded', function() {
// Initialize Feather Icons
if (typeof feather !== 'undefined') {
feather.replace();
}
// Contact Form Handling
const contactForm = document.getElementById('contactForm');
const formSuccess = document.getElementById('formSuccess');
const formError = document.getElementById('formError');
if (contactForm) {
contactForm.addEventListener('submit', async function(e) {
e.preventDefault();
// Get form data
const formData = new FormData(contactForm);
const data = Object.fromEntries(formData);
// Simple validation
if (!data.name || !data.email || !data.service || !data.message) {
showFormError('Please fill in all required fields.');
return;
}
// Disable submit button and show loading
const submitBtn = contactForm.querySelector('button[type="submit"]');
const originalText = submitBtn.innerHTML;
submitBtn.innerHTML = '<i data-feather="loader" class="w-5 h-5 mr-2 animate-spin"></i>Sending...';
submitBtn.disabled = true;
try {
// In a real application, this would be an API call
// For demo purposes, we'll simulate an API call
await simulateApiCall(data);
// Show success message
showFormSuccess();
contactForm.reset();
// Hide success message after 5 seconds
setTimeout(() => {
formSuccess.classList.add('hidden');
}, 5000);
} catch (error) {
showFormError('Failed to send message. Please try again.');
} finally {
// Reset button
submitBtn.innerHTML = originalText;
submitBtn.disabled = false;
feather.replace();
}
});
}
// Form success/error functions
function showFormSuccess() {
formSuccess.classList.remove('hidden');
formError.classList.add('hidden');
}
function showFormError(message) {
formError.classList.remove('hidden');
formSuccess.classList.add('hidden');
if (message && formError.querySelector('p')) {
formError.querySelector('p').textContent = message;
}
}
// Simulate API call
function simulateApiCall(data) {
return new Promise((resolve) => {
setTimeout(() => {
console.log('Form submitted with data:', data);
resolve({ success: true });
}, 1500);
});
}
// Animate stats counters on scroll
const stats = document.querySelectorAll('.counter');
const observerOptions = {
threshold: 0.5,
rootMargin: '0px 0px -50px 0px'
};
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const stat = entry.target;
const targetValue = parseInt(stat.textContent);
if (!stat.hasAttribute('data-animated')) {
animateCounter(stat, targetValue);
stat.setAttribute('data-animated', 'true');
}
}
});
}, observerOptions);
stats.forEach(stat => observer.observe(stat));
function animateCounter(element, target) {
let current = 0;
const increment = target / 50;
const timer = setInterval(() => {
current += increment;
if (current >= target) {
current = target;
clearInterval(timer);
}
element.textContent = Math.round(current) + (element.textContent.includes('+') ? '+' :
element.textContent.includes('x') ? 'x' : '');
}, 30);
}
// Smooth scroll for anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function(e) {
const href = this.getAttribute('href');
// Skip if it's just "#"
if (href === '#') return;
const targetElement = document.querySelector(href);
if (targetElement) {
e.preventDefault();
const headerHeight = document.querySelector('custom-navbar')?.offsetHeight || 80;
const targetPosition = targetElement.getBoundingClientRect().top + window.pageYOffset - headerHeight;
window.scrollTo({
top: targetPosition,
behavior: 'smooth'
});
// Update URL without page reload
history.pushState(null, null, href);
}
});
});
// Add active class to nav links based on scroll position
const sections = document.querySelectorAll('section[id]');
const navLinks = document.querySelectorAll('custom-navbar a[href^="#"]');
function highlightNavLink() {
let current = '';
const scrollPos = window.scrollY + 100;
sections.forEach(section => {
const sectionTop = section.offsetTop;
const sectionHeight = section.clientHeight;
if (scrollPos >= sectionTop && scrollPos < sectionTop + sectionHeight) {
current = section.getAttribute('id');
}
});
navLinks.forEach(link => {
link.classList.remove('active');
if (link.getAttribute('href') === `#${current}`) {
link.classList.add('active');
}
});
}
window.addEventListener('scroll', highlightNavLink);
// Parallax effect for hero section
const heroSection = document.querySelector('section:first-of-type');
if (heroSection) {
window.addEventListener('scroll', () => {
const scrolled = window.pageYOffset;
const rate = scrolled * -0.5;
heroSection.style.transform = `translate3d(0px, ${rate}px, 0px)`;
});
}
// Initialize tooltips for icons
const tooltips = document.querySelectorAll('[data-tooltip]');
tooltips.forEach(element => {
element.addEventListener('mouseenter', function() {
const tooltip = document.createElement('div');
tooltip.className = 'absolute z-50 px-3 py-2 text-sm font-medium text-white bg-gray-900 rounded-lg shadow-sm tooltip';
tooltip.textContent = this.getAttribute('data-tooltip');
tooltip.style.top = `${this.getBoundingClientRect().top - 40}px`;
tooltip.style.left = `${this.getBoundingClientRect().left + this.offsetWidth / 2}px`;
tooltip.style.transform = 'translateX(-50%)';
tooltip.setAttribute('id', 'current-tooltip');
document.body.appendChild(tooltip);
});
element.addEventListener('mouseleave', function() {
const tooltip = document.getElementById('current-tooltip');
if (tooltip) {
tooltip.remove();
}
});
});
// Add fade-in animations to elements on scroll
const fadeElements = document.querySelectorAll('.fade-in-up');
const fadeObserver = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('animate-fade-in');
}
});
}, {
threshold: 0.1,
rootMargin: '0px 0px -50px 0px'
});
fadeElements.forEach(element => fadeObserver.observe(element));
// Form validation improvements
const emailInput = document.getElementById('email');
if (emailInput) {
emailInput.addEventListener('blur', function() {
const email = this.value;
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (email && !emailRegex.test(email)) {
this.classList.add('border-red-500');
showInputError(this, 'Please enter a valid email address.');
} else {
this.classList.remove('border-red-500');
removeInputError(this);
}
});
}
function showInputError(input, message) {
removeInputError(input);
const errorDiv = document.createElement('div');
errorDiv.className = 'mt-1 text-sm text-red-400';
errorDiv.textContent = message;
errorDiv.id = `${input.id}-error`;
input.parentNode.appendChild(errorDiv);
}
function removeInputError(input) {
const existingError = document.getElementById(`${input.id}-error`);
if (existingError) {
existingError.remove();
}
}
// Initialize service dropdown with Feather icon
const serviceSelect = document.getElementById('service');
if (serviceSelect) {
serviceSelect.addEventListener('change', function() {
if (this.value) {
this.classList.remove('text-gray-500');
this.classList.add('text-white');
} else {
this.classList.add('text-gray-500');
this.classList.remove('text-white');
}
});
}
});
// Utility function to debounce scroll events
function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
// Export functions for use in components if needed
window.appUtils = {
debounce,
animateCounter: function(element, target) {
let current = 0;
const increment = target / 50;
const timer = setInterval(() => {
current += increment;
if (current >= target) {
current = target;
clearInterval(timer);
}
element.textContent = Math.round(current) + (element.textContent.includes('+') ? '+' :
element.textContent.includes('x') ? 'x' : '');
}, 30);
}
}; |