75 lines
2.3 KiB
JavaScript
75 lines
2.3 KiB
JavaScript
// 导航栏滚动效果
|
|
window.addEventListener('scroll', function() {
|
|
const header = document.querySelector('header');
|
|
if (window.scrollY > 50) {
|
|
header.style.background = 'rgba(255, 255, 255, 0.95)';
|
|
header.style.boxShadow = '0 2px 4px rgba(0,0,0,0.1)';
|
|
} else {
|
|
header.style.background = 'var(--background)';
|
|
header.style.boxShadow = 'none';
|
|
}
|
|
});
|
|
|
|
// 移动端菜单
|
|
function initMobileMenu() {
|
|
const menuButton = document.querySelector('.menu-button');
|
|
const nav = document.querySelector('nav');
|
|
const overlay = document.querySelector('.overlay');
|
|
const navLinks = document.querySelectorAll('nav a');
|
|
|
|
if (!menuButton || !nav || !overlay) return;
|
|
|
|
menuButton.addEventListener('click', () => {
|
|
menuButton.classList.toggle('active');
|
|
nav.classList.toggle('active');
|
|
overlay.classList.toggle('active');
|
|
document.body.style.overflow = nav.classList.contains('active') ? 'hidden' : '';
|
|
});
|
|
|
|
// 点击蒙层关闭菜单
|
|
overlay.addEventListener('click', () => {
|
|
menuButton.classList.remove('active');
|
|
nav.classList.remove('active');
|
|
overlay.classList.remove('active');
|
|
document.body.style.overflow = '';
|
|
});
|
|
|
|
// 点击导航链接关闭菜单
|
|
navLinks.forEach(link => {
|
|
link.addEventListener('click', () => {
|
|
menuButton.classList.remove('active');
|
|
nav.classList.remove('active');
|
|
overlay.classList.remove('active');
|
|
document.body.style.overflow = '';
|
|
});
|
|
});
|
|
}
|
|
|
|
// 平滑滚动
|
|
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
|
|
anchor.addEventListener('click', function (e) {
|
|
e.preventDefault();
|
|
const target = document.querySelector(this.getAttribute('href'));
|
|
if (target) {
|
|
target.scrollIntoView({
|
|
behavior: 'smooth'
|
|
});
|
|
}
|
|
});
|
|
});
|
|
|
|
// 产品卡片悬停效果
|
|
document.querySelectorAll('.product-card').forEach(card => {
|
|
card.addEventListener('mouseenter', function() {
|
|
this.style.transform = 'translateY(-10px)';
|
|
});
|
|
|
|
card.addEventListener('mouseleave', function() {
|
|
this.style.transform = 'translateY(0)';
|
|
});
|
|
});
|
|
|
|
// 页面加载完成后初始化
|
|
document.addEventListener('DOMContentLoaded', function() {
|
|
initMobileMenu();
|
|
});
|