/** * Roofing Elite - Main Interaction Script */ document.addEventListener('DOMContentLoaded', () => { // 1. Mobile Menu Toggle const mobileMenuBtn = document.getElementById('mobile-menu-btn'); const mobileMenu = document.getElementById('mobile-menu'); if (mobileMenuBtn && mobileMenu) { mobileMenuBtn.addEventListener('click', () => { mobileMenu.classList.toggle('hidden'); // Add a small timeout to allow display:block to apply before toggling classes if using max-height // However, Tailwind uses display:block/none. We handle the animation via custom class if needed. // Simple toggle for this implementation: if (mobileMenu.classList.contains('hidden')) { mobileMenu.style.opacity = '0'; setTimeout(() => { mobileMenu.style.display = 'none'; }, 300); } else { mobileMenu.style.display = 'block'; setTimeout(() => { mobileMenu.style.opacity = '1'; }, 10); } }); } // 2. Scroll Reveal Animation const revealElements = document.querySelectorAll('.reveal'); const revealObserver = new IntersectionObserver((entries, observer) => { entries.forEach(entry => { if (entry.isIntersecting) { entry.target.classList.add('active'); // Optional: Stop observing once revealed // observer.unobserve(entry.target); } }); }, { root: null, threshold: 0.15, // Trigger when 15% of element is visible rootMargin: "0px 0px -50px 0px" }); revealElements.forEach(el => revealObserver.observe(el)); // 3. Animated Number Counters const counters = document.querySelectorAll('.counter'); let hasAnimatedCounters = false; const countObserver = new IntersectionObserver((entries, observer) => { if (hasAnimatedCounters) return; // Only run once entries.forEach(entry => { if (entry.isIntersecting) { hasAnimatedCounters = true; counters.forEach(counter => { const target = +counter.getAttribute('data-target'); const duration = 2000; // Animation duration in ms const increment = target / (duration / 16); // 60fps let current = 0; const updateCounter = () => { current += increment; if (current < target) { // Format numbers with commas counter.innerText = Math.ceil(current).toLocaleString(); requestAnimationFrame(updateCounter); } else { counter.innerText = target.toLocaleString(); // Special formatting for the dollar sign case if (counter.parentElement.parentElement.querySelector('span:contains("Savings")')) { // Logic handled by structure, just ensuring final value } } }; updateCounter(); }); observer.unobserve(entry.target); } }); }, { threshold: 0.5 }); const statsSection = document.getElementById('stats'); if (statsSection) { countObserver.observe(statsSection); } // 4. Navbar Scroll Effect const navbar = document.getElementById('navbar'); window.addEventListener('scroll', () => { if (window.scrollY > 50) { navbar.classList.add('shadow-lg', 'bg-brand-black/95'); navbar.classList.remove('bg-transparent'); } else { navbar.classList.remove('shadow-lg', 'bg-brand-black/95'); navbar.classList.add('bg-transparent'); } }); // 5. Form Handling (Demo) const contactForm = document.querySelector('form'); if (contactForm) { contactForm.addEventListener('submit', (e) => { e.preventDefault(); const btn = contactForm.querySelector('button[type="submit"]'); const originalText = btn.innerText; btn.innerText = 'Request Sent!'; btn.classList.add('bg-green-500', 'text-white'); btn.classList.remove('bg-brand-amber', 'text-brand-black'); setTimeout(() => { btn.innerText = originalText; btn.classList.remove('bg-green-500', 'text-white'); btn.classList.add('bg-brand-amber', 'text-brand-black'); contactForm.reset(); }, 3000); }); } });