// Smooth scrolling for anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
// Close mobile menu if open
const header = document.querySelector('custom-header');
if (header) {
const shadowRoot = header.shadowRoot;
const navMenu = shadowRoot.getElementById('navMenu');
const menuBtn = shadowRoot.getElementById('menuBtn');
if (navMenu.classList.contains('active')) {
navMenu.classList.remove('active');
const menuIcon = menuBtn.querySelector('i');
menuIcon.setAttribute('data-feather', 'menu');
feather.replace();
}
}
document.querySelector(this.getAttribute('href')).scrollIntoView({
behavior: 'smooth'
});
});
});
// Form submission handling with better UX
const contactForm = document.querySelector('#contact form');
if (contactForm) {
contactForm.addEventListener('submit', async function(e) {
e.preventDefault();
const submitBtn = this.querySelector('button[type="submit"]');
const originalBtnText = submitBtn.innerHTML;
try {
// Show loading state
submitBtn.innerHTML = `
Отправка...
`;
submitBtn.disabled = true;
// Simulate API call
await new Promise(resolve => setTimeout(resolve, 1500));
// Show success
const toast = document.createElement('div');
toast.className = 'fixed top-6 right-6 bg-green-500 text-white px-6 py-3 rounded-lg shadow-lg flex items-center animate-fade-in-up z-50';
toast.innerHTML = `
Сообщение отправлено! Мы свяжемся с вами в ближайшее время.
`;
document.body.appendChild(toast);
feather.replace();
// Remove toast after 5s
setTimeout(() => {
toast.classList.add('opacity-0', 'transition-opacity', 'duration-300');
setTimeout(() => toast.remove(), 300);
}, 5000);
// Reset form
this.reset();
} catch (error) {
console.error('Form submission error:', error);
} finally {
submitBtn.innerHTML = originalBtnText;
submitBtn.disabled = false;
}
});
}
// Enhanced animation on scroll
const observerOptions = {
root: null,
rootMargin: '0px',
threshold: 0.1
};
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('animate-fade-in-up');
observer.unobserve(entry.target);
}
});
}, observerOptions);
document.querySelectorAll('[class*="animate-"]').forEach(el => {
observer.observe(el);
});
// Initialize tooltips
document.querySelectorAll('[data-tooltip]').forEach(el => {
const tooltip = document.createElement('div');
tooltip.className = 'absolute hidden bg-gray-900 text-white text-xs rounded py-1 px-2 whitespace-nowrap z-50';
tooltip.textContent = el.getAttribute('data-tooltip');
document.body.appendChild(tooltip);
el.addEventListener('mouseenter', () => {
const rect = el.getBoundingClientRect();
tooltip.style.top = `${rect.top - 30}px`;
tooltip.style.left = `${rect.left + rect.width/2 - tooltip.offsetWidth/2}px`;
tooltip.classList.remove('hidden');
});
el.addEventListener('mouseleave', () => {
tooltip.classList.add('hidden');
});
});