38211-vm/assets/js/main.js
Flatlogic Bot ee218912b8 开发
2026-02-05 12:08:04 +00:00

192 lines
6.1 KiB
JavaScript

document.addEventListener('DOMContentLoaded', () => {
// Navbar Scroll Effect
const navbar = document.querySelector('.navbar');
window.addEventListener('scroll', () => {
if (window.scrollY > 50) {
navbar.classList.add('scrolled');
} else {
navbar.classList.remove('scrolled');
}
});
// Mobile Menu Toggle
const mobileMenuToggle = document.querySelector('.mobile-menu-toggle');
const navLinks = document.querySelector('.nav-links');
if (mobileMenuToggle) {
mobileMenuToggle.addEventListener('click', () => {
navLinks.classList.toggle('active');
const icon = mobileMenuToggle.querySelector('i');
if (navLinks.classList.contains('active')) {
icon.classList.replace('fa-bars', 'fa-times');
} else {
icon.classList.replace('fa-times', 'fa-bars');
}
});
}
// Hero Swiper
const heroSwiper = new Swiper('.hero-swiper', {
loop: true,
effect: 'fade',
fadeEffect: {
crossFade: true
},
autoplay: {
delay: 6000,
disableOnInteraction: false,
},
pagination: {
el: '.swiper-pagination',
clickable: true,
},
navigation: {
nextEl: '.swiper-button-next',
prevEl: '.swiper-button-prev',
},
});
// Testimonials Swiper
const testimonialsSwiper = new Swiper('.testimonials-swiper', {
slidesPerView: 1,
spaceBetween: 30,
loop: true,
autoplay: {
delay: 5000,
disableOnInteraction: false,
},
pagination: {
el: '.swiper-pagination',
clickable: true,
},
breakpoints: {
768: {
slidesPerView: 2,
},
1024: {
slidesPerView: 3,
},
},
});
// Portfolio Filtering
const filterBtns = document.querySelectorAll('.filter-btn');
const portfolioItems = document.querySelectorAll('.portfolio-item');
filterBtns.forEach(btn => {
btn.addEventListener('click', () => {
// Update active button
filterBtns.forEach(b => b.classList.remove('active'));
btn.classList.add('active');
const filterValue = btn.getAttribute('data-filter');
portfolioItems.forEach(item => {
if (filterValue === 'all' || item.classList.contains(filterValue)) {
item.style.display = 'block';
item.style.animation = 'fadeIn 0.5s ease forwards';
} else {
item.style.display = 'none';
}
});
});
});
// Contact Form Submission
const contactForm = document.getElementById('contactForm');
const toast = document.getElementById('toast');
if (contactForm) {
contactForm.addEventListener('submit', async (e) => {
e.preventDefault();
const formData = new FormData(contactForm);
const submitBtn = contactForm.querySelector('button[type="submit"]');
const originalBtnText = submitBtn.innerHTML;
submitBtn.disabled = true;
submitBtn.innerHTML = '<i class="fas fa-spinner fa-spin"></i> 发送中...';
try {
const response = await fetch('contact.php', {
method: 'POST',
body: formData
});
const result = await response.json();
if (result.success) {
showToast('消息已成功发送!我会尽快给您回复。');
contactForm.reset();
} else {
showToast('发送失败: ' + (result.error || '请稍后再试'));
}
} catch (error) {
showToast('无法连接到服务器,请检查网络。');
} finally {
submitBtn.disabled = false;
submitBtn.innerHTML = originalBtnText;
}
});
}
function showToast(message) {
toast.innerText = message;
toast.style.display = 'block';
toast.style.animation = 'slideInUp 0.5s ease forwards';
setTimeout(() => {
toast.style.animation = 'slideOutDown 0.5s ease forwards';
setTimeout(() => {
toast.style.display = 'none';
}, 500);
}, 5000);
}
// Smooth scroll for nav links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
// Close mobile menu if open
if (navLinks.classList.contains('active')) {
navLinks.classList.remove('active');
const icon = mobileMenuToggle.querySelector('i');
icon.classList.replace('fa-times', 'fa-bars');
}
const target = document.querySelector(this.getAttribute('href'));
if (target) {
const offset = 80;
const bodyRect = document.body.getBoundingClientRect().top;
const elementRect = target.getBoundingClientRect().top;
const elementPosition = elementRect - bodyRect;
const offsetPosition = elementPosition - offset;
window.scrollTo({
top: offsetPosition,
behavior: 'smooth'
});
}
});
});
});
// Animation styles
const style = document.createElement('style');
style.textContent = `
@keyframes fadeIn {
from { opacity: 0; transform: translateY(20px); }
to { opacity: 1; transform: translateY(0); }
}
@keyframes slideInUp {
from { transform: translateY(100%); opacity: 0; }
to { transform: translateY(0); opacity: 1; }
}
@keyframes slideOutDown {
from { transform: translateY(0); opacity: 1; }
to { transform: translateY(100%); opacity: 0; }
}
`;
document.head.appendChild(style);