Spaces:
Running
Running
File size: 4,807 Bytes
6f07941 | 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 | /**
* 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);
});
}
}); |