LeosVoiceRedesign / script.js
phallout's picture
Update script.js
54469c9 verified
Raw History Blame Contribute Delete
6.95 kB
// Shared JavaScript for Leo's Voice Website
// Initialize Feather Icons
document.addEventListener('DOMContentLoaded', function() {
if (typeof feather !== 'undefined') {
feather.replace();
}
// Smooth scrolling for anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const targetId = this.getAttribute('href');
if (targetId === '#') return;
const targetElement = document.querySelector(targetId);
if (targetElement) {
const headerHeight = document.querySelector('leo-navbar')?.offsetHeight || 80;
const targetPosition = targetElement.getBoundingClientRect().top + window.pageYOffset - headerHeight;
window.scrollTo({
top: targetPosition,
behavior: 'smooth'
});
// Update URL without page jump
history.pushState(null, null, targetId);
}
});
});
// Navbar scroll effect
const navbar = document.querySelector('leo-navbar');
if (navbar) {
let lastScrollTop = 0;
window.addEventListener('scroll', function() {
const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
const navbarShadow = navbar.querySelector('.navbar-container');
if (scrollTop > 100) {
navbarShadow.classList.add('shadow-lg', 'bg-white/95', 'backdrop-blur-sm');
navbarShadow.classList.remove('bg-transparent');
} else {
navbarShadow.classList.remove('shadow-lg', 'bg-white/95', 'backdrop-blur-sm');
navbarShadow.classList.add('bg-transparent');
}
lastScrollTop = scrollTop;
});
}
// Parallax effect for hero background
const heroSection = document.querySelector('.relative.bg-gradient-to-br');
if (heroSection) {
window.addEventListener('scroll', function() {
const scrolled = window.pageYOffset;
const rate = scrolled * -0.5;
const bgElement = heroSection.querySelector('.absolute.inset-0');
if (bgElement) {
bgElement.style.transform = `translateY(${rate}px)`;
}
});
}
// Intersection Observer for fade-in animations
const observerOptions = {
threshold: 0.1,
rootMargin: '0px 0px -50px 0px'
};
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('animate-fade-in-up');
}
});
}, observerOptions);
// Observe all sections and cards
document.querySelectorAll('section, .bg-white.rounded-2xl').forEach(el => {
observer.observe(el);
});
// Newsletter form submission
const newsletterForm = document.getElementById('newsletterForm');
if (newsletterForm) {
newsletterForm.addEventListener('submit', function(e) {
e.preventDefault();
const email = this.querySelector('input[type="email"]').value;
// Simulate form submission
const submitBtn = this.querySelector('button[type="submit"]');
const originalText = submitBtn.innerHTML;
submitBtn.innerHTML = '<i data-feather="check" class="inline mr-2"></i> Subscribed!';
submitBtn.disabled = true;
submitBtn.classList.add('bg-green-600');
// Reset after 3 seconds
setTimeout(() => {
submitBtn.innerHTML = originalText;
submitBtn.disabled = false;
submitBtn.classList.remove('bg-green-600');
this.reset();
}, 3000);
// In a real app, you would send this to your backend
console.log('Newsletter subscription:', email);
});
}
// Donation amount selection
const donationAmounts = document.querySelectorAll('[data-donation-amount]');
donationAmounts.forEach(amount => {
amount.addEventListener('click', function() {
donationAmounts.forEach(a => a.classList.remove('border-primary-600', 'bg-primary-50'));
this.classList.add('border-primary-600', 'bg-primary-50');
const customAmount = document.getElementById('customAmount');
if (customAmount) {
customAmount.value = this.getAttribute('data-donation-amount');
}
});
});
// Custom donation amount input
const customAmountInput = document.getElementById('customAmount');
if (customAmountInput) {
customAmountInput.addEventListener('input', function() {
donationAmounts.forEach(a => a.classList.remove('border-primary-600', 'bg-primary-50'));
});
}
});
// Debounce function for scroll events
function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
// Copy to clipboard functionality
function copyToClipboard(text) {
navigator.clipboard.writeText(text).then(() => {
// Show success message
const toast = document.createElement('div');
toast.className = 'fixed bottom-4 right-4 bg-green-500 text-white px-6 py-3 rounded-xl shadow-lg z-50 animate-fade-in-up';
toast.innerHTML = '<i data-feather="check" class="inline mr-2"></i> Copied to clipboard!';
document.body.appendChild(toast);
feather.replace();
setTimeout(() => {
toast.remove();
}, 3000);
}).catch(err => {
console.error('Failed to copy: ', err);
});
}
// Form validation helper
function validateEmail(email) {
const re = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
return re.test(email);
}
// Add active class to current page in navigation
function setActiveNavLink() {
const currentPath = window.location.pathname;
const navLinks = document.querySelectorAll('nav a');
navLinks.forEach(link => {
const linkPath = link.getAttribute('href');
if (linkPath === currentPath || (currentPath.includes(linkPath) && linkPath !== '/')) {
link.classList.add('text-primary-600', 'font-bold');
link.classList.remove('text-gray-700');
}
});
}
// Initialize when DOM is loaded
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', setActiveNavLink);
} else {
setActiveNavLink();
}