// Wait for the DOM to be fully loaded document.addEventListener('DOMContentLoaded', function() { // Form validation const form = document.getElementById('contactForm'); if (form) { // Máscara para o telefone const telefone = document.getElementById('telefone'); if (telefone) { telefone.addEventListener('input', function(e) { let value = e.target.value.replace(/\D/g, ''); if (value.length <= 11) { if (value.length > 2) { value = '(' + value.substring(0,2) + ') ' + value.substring(2); } if (value.length > 9) { value = value.substring(0,10) + '-' + value.substring(10); } e.target.value = value; } }); } form.addEventListener('submit', function(event) { event.preventDefault(); let isValid = true; const requiredFields = form.querySelectorAll('[required]'); // Remove todas as classes de validação form.querySelectorAll('.is-invalid').forEach(field => { field.classList.remove('is-invalid'); }); // Valida campos obrigatórios requiredFields.forEach(field => { if (!field.value.trim()) { field.classList.add('is-invalid'); isValid = false; } }); // Valida email const email = form.querySelector('#email'); if (email && email.value.trim()) { const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; if (!emailRegex.test(email.value.trim())) { email.classList.add('is-invalid'); isValid = false; } } // Valida telefone (se preenchido) const telefone = form.querySelector('#telefone'); if (telefone && telefone.value.trim()) { const telefoneRegex = /^\([0-9]{2}\) [0-9]{5}-[0-9]{4}$/; if (!telefoneRegex.test(telefone.value.trim())) { telefone.classList.add('is-invalid'); isValid = false; } } if (isValid) { // Aqui você pode adicionar o código para enviar o formulário alert('Mensagem enviada com sucesso!'); form.reset(); } }); } // Smooth scrolling for all links document.querySelectorAll('a[href^="#"]').forEach(anchor => { anchor.addEventListener('click', function (e) { e.preventDefault(); const target = document.querySelector(this.getAttribute('href')); if (target) { target.scrollIntoView({ behavior: 'smooth', block: 'start' }); } }); }); // Highlight active navigation item based on scroll position const sections = document.querySelectorAll('section'); const navLinks = document.querySelectorAll('.nav-link'); function updateActiveNav() { let current = ''; sections.forEach(section => { const sectionTop = section.offsetTop; const sectionHeight = section.clientHeight; if (window.pageYOffset >= sectionTop - 60) { current = section.getAttribute('id'); } }); navLinks.forEach(link => { link.classList.remove('active'); if (link.getAttribute('href') === `#${current}`) { link.classList.add('active'); } }); } // Update active nav on scroll window.addEventListener('scroll', updateActiveNav); updateActiveNav(); // Initial call // Add animation to cards when they come into view const cards = document.querySelectorAll('.card'); const observerOptions = { threshold: 0.1 }; const observer = new IntersectionObserver((entries) => { entries.forEach(entry => { if (entry.isIntersecting) { entry.target.style.opacity = '1'; entry.target.style.transform = 'translateY(0)'; } }); }, observerOptions); cards.forEach(card => { card.style.opacity = '0'; card.style.transform = 'translateY(20px)'; card.style.transition = 'opacity 0.5s ease, transform 0.5s ease'; observer.observe(card); }); });