文件

245 行
7.1 KiB
JavaScript

// 主要JavaScript功能文件
// DOM加载完成后执行
document.addEventListener('DOMContentLoaded', function() {
// 初始化所有功能
initNavbar();
initScrollAnimations();
initFloatingCards();
initSmoothScroll();
});
// 导航栏功能
function initNavbar() {
const navbar = document.querySelector('.navbar');
const navLinks = document.querySelectorAll('.nav-link');
// 滚动时改变导航栏样式
window.addEventListener('scroll', function() {
if (window.scrollY > 50) {
navbar.style.background = 'rgba(255, 255, 255, 0.98)';
navbar.style.boxShadow = '0 4px 6px -1px rgb(0 0 0 / 0.1)';
} else {
navbar.style.background = 'rgba(255, 255, 255, 0.95)';
navbar.style.boxShadow = 'none';
}
});
// 导航链接点击效果
navLinks.forEach(link => {
link.addEventListener('click', function(e) {
// 移除所有活动状态
navLinks.forEach(l => l.classList.remove('active'));
// 添加当前活动状态
this.classList.add('active');
});
});
}
// 滚动动画
function initScrollAnimations() {
// 创建Intersection Observer
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.style.opacity = '1';
entry.target.style.transform = 'translateY(0)';
}
});
}, {
threshold: 0.1,
rootMargin: '0px 0px -50px 0px'
});
// 观察需要动画的元素
const animatedElements = document.querySelectorAll(
'.solution-card, .comparison-side, .flow-item, .concept-content'
);
animatedElements.forEach(el => {
// 设置初始状态
el.style.opacity = '0';
el.style.transform = 'translateY(30px)';
el.style.transition = 'opacity 0.6s ease, transform 0.6s ease';
// 开始观察
observer.observe(el);
});
}
// 浮动卡片动画增强
function initFloatingCards() {
const cards = document.querySelectorAll('.floating-card');
cards.forEach((card, index) => {
// 鼠标悬停效果
card.addEventListener('mouseenter', function() {
this.style.transform = 'translateY(-10px) scale(1.05)';
this.style.boxShadow = '0 20px 25px -5px rgb(0 0 0 / 0.2)';
});
card.addEventListener('mouseleave', function() {
this.style.transform = 'translateY(0) scale(1)';
this.style.boxShadow = '0 20px 25px -5px rgb(0 0 0 / 0.1)';
});
// 随机浮动动画
setInterval(() => {
const randomY = Math.random() * 20 - 10;
const randomX = Math.random() * 10 - 5;
card.style.transform += ` translate(${randomX}px, ${randomY}px)`;
setTimeout(() => {
card.style.transform = card.style.transform.replace(
/translate\([^)]*\)/g, ''
);
}, 2000);
}, 3000 + index * 1000);
});
}
// 平滑滚动
function initSmoothScroll() {
// 为所有内部链接添加平滑滚动
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',
block: 'start'
});
}
});
});
}
// 按钮点击效果
document.addEventListener('click', function(e) {
if (e.target.classList.contains('btn')) {
// 创建波纹效果
const ripple = document.createElement('span');
const rect = e.target.getBoundingClientRect();
const size = Math.max(rect.width, rect.height);
const x = e.clientX - rect.left - size / 2;
const y = e.clientY - rect.top - size / 2;
ripple.style.cssText = `
position: absolute;
width: ${size}px;
height: ${size}px;
left: ${x}px;
top: ${y}px;
background: rgba(255, 255, 255, 0.3);
border-radius: 50%;
transform: scale(0);
animation: ripple 0.6s linear;
pointer-events: none;
`;
e.target.style.position = 'relative';
e.target.style.overflow = 'hidden';
e.target.appendChild(ripple);
setTimeout(() => {
ripple.remove();
}, 600);
}
});
// 添加CSS动画关键帧
const style = document.createElement('style');
style.textContent = `
@keyframes ripple {
to {
transform: scale(4);
opacity: 0;
}
}
@keyframes fadeInUp {
from {
opacity: 0;
transform: translateY(30px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.animate-fade-in-up {
animation: fadeInUp 0.6s ease forwards;
}
`;
document.head.appendChild(style);
// 性能优化:节流函数
function throttle(func, limit) {
let inThrottle;
return function() {
const args = arguments;
const context = this;
if (!inThrottle) {
func.apply(context, args);
inThrottle = true;
setTimeout(() => inThrottle = false, limit);
}
};
}
// 优化滚动事件
window.addEventListener('scroll', throttle(function() {
// 滚动到顶部按钮显示/隐藏
const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
// 可以在这里添加更多滚动相关的功能
}, 100));
// 响应式菜单(移动端)
function initMobileMenu() {
const navMenu = document.querySelector('.nav-menu');
const mobileMenuBtn = document.createElement('button');
mobileMenuBtn.innerHTML = '☰';
mobileMenuBtn.className = 'mobile-menu-btn';
mobileMenuBtn.style.cssText = `
display: none;
background: none;
border: none;
font-size: 1.5rem;
color: var(--text-primary);
cursor: pointer;
`;
// 在移动端显示菜单按钮
const mediaQuery = window.matchMedia('(max-width: 768px)');
function handleMediaQuery(e) {
if (e.matches) {
mobileMenuBtn.style.display = 'block';
document.querySelector('.nav-container').appendChild(mobileMenuBtn);
} else {
mobileMenuBtn.style.display = 'none';
}
}
mediaQuery.addListener(handleMediaQuery);
handleMediaQuery(mediaQuery);
// 菜单切换功能
mobileMenuBtn.addEventListener('click', function() {
navMenu.style.display = navMenu.style.display === 'flex' ? 'none' : 'flex';
});
}
// 初始化移动端菜单
initMobileMenu();
// 错误处理
window.addEventListener('error', function(e) {
console.error('JavaScript错误:', e.error);
});
// 页面加载完成提示
console.log('码上有酒 - 新零售系统已加载完成');