// 主要JavaScript功能文件 document.addEventListener('DOMContentLoaded', function() { // 初始化所有功能 initMobileMenu(); initScrollReveal(); initParticleBackground(); initSmoothScroll(); initCounterAnimation(); initNavigationEffect(); }); // 移动端菜单功能 function initMobileMenu() { const mobileMenuBtn = document.getElementById('mobile-menu-btn'); const mobileMenu = document.getElementById('mobile-menu'); if (mobileMenuBtn && mobileMenu) { mobileMenuBtn.addEventListener('click', function() { mobileMenu.classList.toggle('hidden'); }); } } // 滚动显示动画 function initScrollReveal() { const revealElements = document.querySelectorAll('.reveal'); const revealObserver = new IntersectionObserver((entries) => { entries.forEach(entry => { if (entry.isIntersecting) { entry.target.classList.add('active'); } }); }, { threshold: 0.1, rootMargin: '0px 0px -50px 0px' }); revealElements.forEach(element => { revealObserver.observe(element); }); } // 粒子背景效果 function initParticleBackground() { const container = document.getElementById('particle-container'); if (!container) return; // 使用PIXI.js创建粒子效果 const app = new PIXI.Application({ width: window.innerWidth, height: window.innerHeight, backgroundColor: 0x000000, backgroundAlpha: 0, antialias: true }); container.appendChild(app.view); // 创建粒子容器 const particleContainer = new PIXI.Container(); app.stage.addChild(particleContainer); // 粒子数组 const particles = []; const particleCount = 50; // 创建粒子 for (let i = 0; i < particleCount; i++) { const particle = new PIXI.Graphics(); particle.beginFill(0xed8936, 0.6); particle.drawCircle(0, 0, Math.random() * 2 + 1); particle.endFill(); particle.x = Math.random() * app.screen.width; particle.y = Math.random() * app.screen.height; particle.vx = (Math.random() - 0.5) * 0.5; particle.vy = (Math.random() - 0.5) * 0.5; particles.push(particle); particleContainer.addChild(particle); } // 动画循环 app.ticker.add(() => { particles.forEach(particle => { particle.x += particle.vx; particle.y += particle.vy; // 边界检测 if (particle.x < 0 || particle.x > app.screen.width) { particle.vx *= -1; } if (particle.y < 0 || particle.y > app.screen.height) { particle.vy *= -1; } // 保持在屏幕内 particle.x = Math.max(0, Math.min(app.screen.width, particle.x)); particle.y = Math.max(0, Math.min(app.screen.height, particle.y)); }); }); // 窗口大小调整 window.addEventListener('resize', () => { app.renderer.resize(window.innerWidth, window.innerHeight); }); } // 平滑滚动 function initSmoothScroll() { const links = document.querySelectorAll('a[href^="#"]'); links.forEach(link => { link.addEventListener('click', function(e) { e.preventDefault(); const targetId = this.getAttribute('href'); const targetElement = document.querySelector(targetId); if (targetElement) { const offsetTop = targetElement.offsetTop - 80; // 考虑导航栏高度 window.scrollTo({ top: offsetTop, behavior: 'smooth' }); } }); }); } // 数字计数动画 function initCounterAnimation() { const counters = [ { id: 'stat-documents', target: 500, suffix: '+' }, { id: 'stat-clients', target: 100, suffix: '+' }, { id: 'stat-uptime', target: 99.9, suffix: '', decimals: 1 }, { id: 'stat-satisfaction', target: 98, suffix: '' } ]; const counterObserver = new IntersectionObserver((entries) => { entries.forEach(entry => { if (entry.isIntersecting) { const counter = counters.find(c => c.id === entry.target.id); if (counter) { animateCounter(counter); counterObserver.unobserve(entry.target); } } }); }, { threshold: 0.5 }); counters.forEach(counter => { const element = document.getElementById(counter.id); if (element) { counterObserver.observe(element); } }); function animateCounter(counter) { const element = document.getElementById(counter.id); if (!element) return; const duration = 2000; // 2秒 const start = performance.now(); const startValue = 0; const endValue = counter.target; function updateCounter(currentTime) { const elapsed = currentTime - start; const progress = Math.min(elapsed / duration, 1); // 使用缓动函数 const easeOutQuart = 1 - Math.pow(1 - progress, 4); const currentValue = startValue + (endValue - startValue) * easeOutQuart; if (counter.decimals) { element.textContent = currentValue.toFixed(counter.decimals) + (counter.suffix || ''); } else { element.textContent = Math.floor(currentValue) + (counter.suffix || ''); } if (progress < 1) { requestAnimationFrame(updateCounter); } } requestAnimationFrame(updateCounter); } } // 导航栏滚动效果 function initNavigationEffect() { const navbar = document.querySelector('nav'); let lastScrollY = window.scrollY; window.addEventListener('scroll', () => { const currentScrollY = window.scrollY; if (currentScrollY > 100) { navbar.classList.add('nav-blur'); } else { navbar.classList.remove('nav-blur'); } // 隐藏/显示导航栏 if (currentScrollY > lastScrollY && currentScrollY > 200) { navbar.style.transform = 'translateY(-100%)'; } else { navbar.style.transform = 'translateY(0)'; } lastScrollY = currentScrollY; }); } // 产品卡片悬停效果 function initProductCards() { const cards = document.querySelectorAll('.card-hover'); cards.forEach(card => { card.addEventListener('mouseenter', function() { anime({ targets: this, scale: 1.05, rotateY: 5, duration: 300, easing: 'easeOutCubic' }); }); card.addEventListener('mouseleave', function() { anime({ targets: this, scale: 1, rotateY: 0, duration: 300, easing: 'easeOutCubic' }); }); }); } // 文字打字机效果 function initTypewriterEffect() { const typewriterElements = document.querySelectorAll('.typewriter'); typewriterElements.forEach(element => { const text = element.textContent; element.textContent = ''; element.style.opacity = '1'; let i = 0; const typeInterval = setInterval(() => { if (i < text.length) { element.textContent += text.charAt(i); i++; } else { clearInterval(typeInterval); } }, 100); }); } // 滚动进度条 function initScrollProgress() { const progressBar = document.createElement('div'); progressBar.style.cssText = ` position: fixed; top: 0; left: 0; width: 0%; height: 3px; background: linear-gradient(90deg, #ed8936, #f6ad55); z-index: 9999; transition: width 0.1s ease; `; document.body.appendChild(progressBar); window.addEventListener('scroll', () => { const scrollTop = window.scrollY; const docHeight = document.documentElement.scrollHeight - window.innerHeight; const scrollPercent = (scrollTop / docHeight) * 100; progressBar.style.width = scrollPercent + '%'; }); } // 鼠标跟随效果 function initMouseFollow() { const cursor = document.createElement('div'); cursor.style.cssText = ` position: fixed; width: 20px; height: 20px; background: radial-gradient(circle, rgba(237, 137, 54, 0.3) 0%, transparent 70%); border-radius: 50%; pointer-events: none; z-index: 9998; transition: transform 0.1s ease; `; document.body.appendChild(cursor); let mouseX = 0; let mouseY = 0; let cursorX = 0; let cursorY = 0; document.addEventListener('mousemove', (e) => { mouseX = e.clientX; mouseY = e.clientY; }); function updateCursor() { cursorX += (mouseX - cursorX) * 0.1; cursorY += (mouseY - cursorY) * 0.1; cursor.style.left = cursorX - 10 + 'px'; cursor.style.top = cursorY - 10 + 'px'; requestAnimationFrame(updateCursor); } updateCursor(); } // 页面加载完成后的初始化 window.addEventListener('load', function() { // 隐藏加载动画 const loader = document.querySelector('.loader'); if (loader) { loader.style.opacity = '0'; setTimeout(() => { loader.style.display = 'none'; }, 500); } // 初始化额外功能 initProductCards(); initScrollProgress(); // 延迟初始化鼠标跟随效果 setTimeout(() => { initMouseFollow(); }, 1000); }); // 错误处理 window.addEventListener('error', function(e) { console.error('JavaScript错误:', e.error); }); // 性能监控 if ('performance' in window) { window.addEventListener('load', function() { setTimeout(() => { const perfData = performance.getEntriesByType('navigation')[0]; console.log('页面加载时间:', perfData.loadEventEnd - perfData.loadEventStart, 'ms'); }, 0); }); }