// Smooth scrolling for anchor links document.querySelectorAll('a[href^="#"]').forEach(anchor => { anchor.addEventListener('click', function(e) { e.preventDefault(); document.querySelector(this.getAttribute('href')).scrollIntoView({ behavior: 'smooth' }); }); }); // Form submission handler const contactForm = document.querySelector('form'); if (contactForm) { contactForm.addEventListener('submit', async (e) => { e.preventDefault(); const formData = new FormData(contactForm); const submitBtn = contactForm.querySelector('button[type="submit"]'); const originalText = submitBtn.innerHTML; try { submitBtn.disabled = true; submitBtn.innerHTML = 'Sending... '; feather.replace(); // Simulate API call await new Promise(resolve => setTimeout(resolve, 1500)); // Show success message alert('Thank you for your message! I will get back to you soon.'); contactForm.reset(); } catch (error) { alert('There was an error sending your message. Please try again later.'); } finally { submitBtn.disabled = false; submitBtn.innerHTML = originalText; feather.replace(); } }); } // Animate elements on scroll const animateOnScroll = () => { const elements = document.querySelectorAll('.animate-fade-in'); elements.forEach(element => { const elementPosition = element.getBoundingClientRect().top; const windowHeight = window.innerHeight; if (elementPosition < windowHeight - 100) { element.style.opacity = '1'; element.style.transform = 'translateY(0)'; } }); }; // Initialize animation and set up scroll listener document.addEventListener('DOMContentLoaded', () => { animateOnScroll(); window.addEventListener('scroll', animateOnScroll); }); // Theme switcher with localStorage const toggleTheme = () => { const html = document.documentElement; html.classList.toggle('light-mode'); const isLight = html.classList.contains('light-mode'); localStorage.setItem('theme', isLight ? 'light' : 'dark'); // Update theme toggle icon const themeIcons = document.querySelectorAll('[data-theme-icon]'); themeIcons.forEach(icon => { if (isLight) { icon.setAttribute('data-feather', 'moon'); } else { icon.setAttribute('data-feather', 'sun'); } feather.replace(icon); }); }; // Initialize theme from localStorage const savedTheme = localStorage.getItem('theme') || 'dark'; if (savedTheme === 'light') { document.documentElement.classList.add('light-mode'); }