// 山东大学公共卫生学科100周年庆典网站 - JavaScript // 轮播图功能 let currentSlide = 0; const slides = document.querySelectorAll('.carousel-item'); function showSlide(index) { // 隐藏所有幻灯片 slides.forEach(slide => slide.classList.remove('active')); // 显示指定幻灯片 if (index >= slides.length) { currentSlide = 0; } else if (index < 0) { currentSlide = slides.length - 1; } else { currentSlide = index; } slides[currentSlide].classList.add('active'); } function changeSlide(direction) { showSlide(currentSlide + direction); } function syncNoticeListHeight() { const carouselElement = document.querySelector('.carousel'); const noticeListElement = document.querySelector('.notice-list'); if (!carouselElement || !noticeListElement) { return; } if (window.innerWidth <= 768) { noticeListElement.style.maxHeight = 'none'; return; } noticeListElement.style.maxHeight = `${carouselElement.offsetHeight}px`; } // 自动轮播(每5秒切换一次) let autoSlideInterval = setInterval(() => { changeSlide(1); }, 5000); // 鼠标悬停时暂停自动轮播 const carousel = document.querySelector('.carousel'); if (carousel) { carousel.addEventListener('mouseenter', () => { clearInterval(autoSlideInterval); }); carousel.addEventListener('mouseleave', () => { autoSlideInterval = setInterval(() => { changeSlide(1); }, 5000); }); } // 页面加载完成后初始化、绑定动画与平滑滚动 document.addEventListener('DOMContentLoaded', () => { if (slides.length > 0) { showSlide(0); } syncNoticeListHeight(); window.addEventListener('resize', syncNoticeListHeight); const banner = document.querySelector('.banner'); const nav = document.querySelector('.main-nav'); // 防止点击导航后滚动监听器覆盖 active 状态 let isClickScrolling = false; let clickScrollTimer = null; let isPageSnapping = false; function setScrollLock(duration = 1000) { isClickScrolling = true; isPageSnapping = true; clearTimeout(clickScrollTimer); clickScrollTimer = setTimeout(() => { isClickScrolling = false; isPageSnapping = false; }, duration); } function scrollToPosition(top) { setScrollLock(); window.scrollTo({ top, behavior: 'smooth' }); } function scrollToSecondPage() { if (!nav) { return; } scrollToPosition(nav.offsetTop); } function scrollToBanner() { scrollToPosition(0); } if (banner && nav) { window.addEventListener('wheel', (event) => { if (isPageSnapping) { event.preventDefault(); return; } const scrollY = window.scrollY; const bannerThreshold = banner.offsetHeight - 40; const snapBackThreshold = banner.offsetHeight * 1.5; if (event.deltaY > 0 && scrollY < bannerThreshold) { event.preventDefault(); scrollToSecondPage(); } else if (event.deltaY < 0 && scrollY < snapBackThreshold && scrollY > 0) { event.preventDefault(); setScrollLock(500); window.scrollTo({ top: 0, behavior: 'smooth' }); } }, { passive: false }); } // 1. 导航栏平滑滚动及高亮更新 document.querySelectorAll('a[href^="#"]').forEach(anchor => { anchor.addEventListener('click', function (e) { e.preventDefault(); const targetId = this.getAttribute('href'); if(targetId === '#') return; const targetElement = document.querySelector(targetId); if (targetElement) { // 减去导航栏高度(约75px),防止导航栏遮挡标题 const headerOffset = 75; const elementPosition = targetElement.getBoundingClientRect().top; const offsetPosition = elementPosition + window.pageYOffset - headerOffset; // 设置点击滚动标记 window.scrollTo({ top: offsetPosition, behavior: "smooth" }); setScrollLock(); // 立即更新活动状态 document.querySelectorAll('.nav-link').forEach(link => { link.classList.remove('active'); }); this.classList.add('active'); } }); }); // 2. 元素入场动画 (Intersection Observer) const observerOptions = { threshold: 0.1, rootMargin: "0px 0px -50px 0px" }; const observer = new IntersectionObserver((entries) => { entries.forEach(entry => { if (entry.isIntersecting) { entry.target.classList.add('visible'); observer.unobserve(entry.target); } }); }, observerOptions); document.querySelectorAll('.scroll-animate').forEach(el => { observer.observe(el); }); // 3. 页面滚动时自动高亮当前所在导航 const sections = document.querySelectorAll('section'); const navLinks = document.querySelectorAll('.nav-link'); window.addEventListener('scroll', () => { // 如果是点击导航触发的滚动,不执行自动更新 if (isClickScrolling) { return; } let current = ''; const scrollY = window.scrollY; // 只有滚动超过banner区域(约100vh)后才开始高亮导航 if (scrollY < window.innerHeight * 0.8) { // 在顶部banner区域时,取消所有active状态 navLinks.forEach(link => { link.classList.remove('active'); }); return; } sections.forEach(section => { const sectionTop = section.offsetTop; const sectionHeight = section.offsetHeight; // 更精确的判断:当前滚动位置在该区域范围内 if (scrollY >= (sectionTop - 200) && scrollY < (sectionTop + sectionHeight - 200)) { current = section.getAttribute('id'); } }); navLinks.forEach(link => { link.classList.remove('active'); if (link.getAttribute('href') === '#' + current && current !== '') { link.classList.add('active'); } }); }); });