206 lines
5.8 KiB
JavaScript
206 lines
5.8 KiB
JavaScript
(function() {
|
|
'use strict';
|
|
|
|
// Respect reduced motion preference
|
|
const prefersReducedMotion = window.matchMedia('(prefers-reduced-motion: reduce)').matches;
|
|
|
|
// Mobile menu toggle
|
|
const menuToggle = document.querySelector('.menu-toggle');
|
|
const mainNav = document.querySelector('.main-nav');
|
|
const navOverlay = document.querySelector('.nav-overlay');
|
|
|
|
function closeMenu() {
|
|
menuToggle.setAttribute('aria-expanded', 'false');
|
|
menuToggle.classList.remove('active');
|
|
mainNav.classList.remove('active');
|
|
navOverlay.classList.remove('active');
|
|
document.body.style.overflow = '';
|
|
}
|
|
|
|
function openMenu() {
|
|
menuToggle.setAttribute('aria-expanded', 'true');
|
|
menuToggle.classList.add('active');
|
|
mainNav.classList.add('active');
|
|
navOverlay.classList.add('active');
|
|
document.body.style.overflow = 'hidden';
|
|
}
|
|
|
|
if (menuToggle && mainNav && navOverlay) {
|
|
menuToggle.addEventListener('click', function() {
|
|
const isExpanded = this.getAttribute('aria-expanded') === 'true';
|
|
if (isExpanded) {
|
|
closeMenu();
|
|
} else {
|
|
openMenu();
|
|
}
|
|
});
|
|
|
|
// Close menu when clicking on a link
|
|
mainNav.querySelectorAll('a').forEach(link => {
|
|
link.addEventListener('click', closeMenu);
|
|
});
|
|
|
|
// Close menu when clicking overlay
|
|
navOverlay.addEventListener('click', closeMenu);
|
|
|
|
// Close menu on escape key
|
|
document.addEventListener('keydown', function(e) {
|
|
if (e.key === 'Escape' && mainNav.classList.contains('active')) {
|
|
closeMenu();
|
|
menuToggle.focus();
|
|
}
|
|
});
|
|
}
|
|
|
|
// Reveal on scroll with IntersectionObserver
|
|
if (!prefersReducedMotion) {
|
|
const revealElements = document.querySelectorAll('.reveal');
|
|
let revealDelay = 0;
|
|
|
|
const revealObserver = new IntersectionObserver((entries) => {
|
|
entries.forEach((entry) => {
|
|
if (entry.isIntersecting) {
|
|
setTimeout(() => {
|
|
entry.target.classList.add('visible');
|
|
}, revealDelay);
|
|
revealDelay += 80;
|
|
setTimeout(() => { revealDelay = 0; }, 500);
|
|
revealObserver.unobserve(entry.target);
|
|
}
|
|
});
|
|
}, {
|
|
threshold: 0.1,
|
|
rootMargin: '0px 0px -50px 0px'
|
|
});
|
|
|
|
revealElements.forEach(el => revealObserver.observe(el));
|
|
} else {
|
|
// If reduced motion is preferred, show all elements immediately
|
|
document.querySelectorAll('.reveal').forEach(el => {
|
|
el.classList.add('visible');
|
|
});
|
|
}
|
|
|
|
// Header scroll effect
|
|
const header = document.querySelector('.site-header');
|
|
|
|
window.addEventListener('scroll', function() {
|
|
const currentScroll = window.pageYOffset;
|
|
|
|
if (currentScroll > 100) {
|
|
header.style.boxShadow = '0 4px 20px rgba(44, 24, 16, 0.1)';
|
|
} else {
|
|
header.style.boxShadow = 'none';
|
|
}
|
|
}, { passive: true });
|
|
|
|
// Smooth scroll for anchor links
|
|
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
|
|
anchor.addEventListener('click', function(e) {
|
|
const href = this.getAttribute('href');
|
|
if (href === '#') return;
|
|
|
|
const target = document.querySelector(href);
|
|
if (target) {
|
|
e.preventDefault();
|
|
const headerHeight = header.offsetHeight;
|
|
const targetPosition = target.getBoundingClientRect().top + window.pageYOffset - headerHeight - 20;
|
|
|
|
window.scrollTo({
|
|
top: targetPosition,
|
|
behavior: prefersReducedMotion ? 'auto' : 'smooth'
|
|
});
|
|
|
|
// Set focus to the target for accessibility
|
|
target.setAttribute('tabindex', '-1');
|
|
target.focus({ preventScroll: true });
|
|
}
|
|
});
|
|
});
|
|
|
|
// Popup functionality
|
|
const popupOverlay = document.getElementById('popupOverlay');
|
|
const popupClose = document.getElementById('popupClose');
|
|
const floatingCta = document.getElementById('floatingCta');
|
|
let popupShown = false;
|
|
|
|
function openPopup() {
|
|
popupOverlay.classList.add('active');
|
|
popupOverlay.setAttribute('aria-hidden', 'false');
|
|
document.body.style.overflow = 'hidden';
|
|
|
|
// Focus the close button for accessibility
|
|
setTimeout(() => {
|
|
popupClose.focus();
|
|
}, 100);
|
|
}
|
|
|
|
function closePopup() {
|
|
popupOverlay.classList.remove('active');
|
|
popupOverlay.setAttribute('aria-hidden', 'true');
|
|
document.body.style.overflow = '';
|
|
|
|
// Return focus to floating button
|
|
floatingCta.focus();
|
|
}
|
|
|
|
// Auto-open popup after 5 seconds
|
|
setTimeout(() => {
|
|
if (!popupShown) {
|
|
popupShown = true;
|
|
openPopup();
|
|
}
|
|
}, 5000);
|
|
|
|
// Floating CTA click
|
|
if (floatingCta) {
|
|
floatingCta.addEventListener('click', openPopup);
|
|
}
|
|
|
|
// Close popup
|
|
if (popupClose) {
|
|
popupClose.addEventListener('click', closePopup);
|
|
}
|
|
|
|
// Close on overlay click
|
|
if (popupOverlay) {
|
|
popupOverlay.addEventListener('click', function(e) {
|
|
if (e.target === popupOverlay) {
|
|
closePopup();
|
|
}
|
|
});
|
|
}
|
|
|
|
// Close on escape key
|
|
document.addEventListener('keydown', function(e) {
|
|
if (e.key === 'Escape' && popupOverlay.classList.contains('active')) {
|
|
closePopup();
|
|
}
|
|
});
|
|
|
|
// Trap focus within popup when open
|
|
if (popupOverlay) {
|
|
const focusableElements = popupOverlay.querySelectorAll(
|
|
'button, [href], input, select, textarea, [tabindex]:not([tabindex="-1"])'
|
|
);
|
|
const firstFocusable = focusableElements[0];
|
|
const lastFocusable = focusableElements[focusableElements.length - 1];
|
|
|
|
popupOverlay.addEventListener('keydown', function(e) {
|
|
if (e.key !== 'Tab') return;
|
|
|
|
if (e.shiftKey) {
|
|
if (document.activeElement === firstFocusable) {
|
|
lastFocusable.focus();
|
|
e.preventDefault();
|
|
}
|
|
} else {
|
|
if (document.activeElement === lastFocusable) {
|
|
firstFocusable.focus();
|
|
e.preventDefault();
|
|
}
|
|
}
|
|
});
|
|
}
|
|
|
|
})(); |