// 科技成果页面 - 3D堆叠轮播脚本 $(document).ready(function() { // 堆叠轮播功能 let currentSlide = 0; let allSlides = $('.achievement-slide'); let filteredSlides = allSlides; // 当前过滤后的幻灯片 let totalSlides = allSlides.length; // 更新幻灯片显示 function updateSlides() { const slides = filteredSlides; const total = slides.length; if (total === 0) return; // 先移除所有幻灯片(包括隐藏的)的状态类 allSlides.removeClass('active prev next prev-2 next-2'); // 当前图片(中间) slides.eq(currentSlide).addClass('active'); // 计算各个位置的索引(使用数组记录已分配的索引,避免重复) const assigned = [currentSlide]; // 已分配的索引 // 计算左侧第1张(prev) let prevIndex = (currentSlide - 1 + total) % total; if (!assigned.includes(prevIndex)) { slides.eq(prevIndex).addClass('prev'); assigned.push(prevIndex); } // 计算右侧第1张(next) let nextIndex = (currentSlide + 1) % total; if (!assigned.includes(nextIndex)) { slides.eq(nextIndex).addClass('next'); assigned.push(nextIndex); } // 只有当总数>=4时才显示prev-2 if (total >= 4) { let prev2Index = (currentSlide - 2 + total) % total; if (!assigned.includes(prev2Index)) { slides.eq(prev2Index).addClass('prev-2'); assigned.push(prev2Index); } } // 只有当总数>=5时才显示next-2 if (total >= 5) { let next2Index = (currentSlide + 2) % total; if (!assigned.includes(next2Index)) { slides.eq(next2Index).addClass('next-2'); } } console.log('当前幻灯片:', currentSlide + 1, '/', total, '已分配位置:', assigned.length); } // 切换到指定幻灯片 function goToSlide(index) { const total = filteredSlides.length; if (total === 0) return; if (index < 0) { currentSlide = total - 1; } else if (index >= total) { currentSlide = 0; } else { currentSlide = index; } updateSlides(); } // 上一张按钮 $('#achievementPrev').on('click', function() { goToSlide(currentSlide - 1); }); // 下一张按钮 $('#achievementNext').on('click', function() { goToSlide(currentSlide + 1); }); // 点击幻灯片切换 $(document).on('click', '.achievement-slide', function() { const clickedIndex = filteredSlides.index(this); if (clickedIndex !== -1 && clickedIndex !== currentSlide) { goToSlide(clickedIndex); } }); // 分类筛选功能 $('.achievement-tab-btn').on('click', function() { // 更新按钮状态 $('.achievement-tab-btn').removeClass('active'); $(this).addClass('active'); const filterType = $(this).data('filter'); // 移除所有幻灯片的隐藏标记 allSlides.removeClass('filtered-hidden'); // 根据筛选条件显示/隐藏幻灯片 if (filterType === 'all') { filteredSlides = allSlides; } else { // 添加隐藏类到不匹配的幻灯片 allSlides.each(function() { if ($(this).data('type') !== filterType) { $(this).addClass('filtered-hidden'); } }); // 筛选匹配的幻灯片 filteredSlides = allSlides.filter('[data-type="' + filterType + '"]'); } // 重置到第一张 currentSlide = 0; // 更新显示 updateSlides(); console.log('筛选类型:', filterType, '共', filteredSlides.length, '项'); }); // 键盘控制 $(document).on('keydown', function(e) { if (e.key === 'ArrowLeft') { goToSlide(currentSlide - 1); } else if (e.key === 'ArrowRight') { goToSlide(currentSlide + 1); } }); // 触摸滑动支持(移动端) let touchStartX = 0; let touchEndX = 0; $('.achievements-slider').on('touchstart', function(e) { touchStartX = e.touches[0].clientX; }); $('.achievements-slider').on('touchend', function(e) { touchEndX = e.changedTouches[0].clientX; handleSwipe(); }); function handleSwipe() { const swipeThreshold = 50; const diff = touchStartX - touchEndX; if (Math.abs(diff) > swipeThreshold) { if (diff > 0) { // 向左滑动,显示下一张 goToSlide(currentSlide + 1); } else { // 向右滑动,显示上一张 goToSlide(currentSlide - 1); } } } // 自动播放(可选) let autoplayInterval; const autoplayDelay = 5000; // 5秒 function startAutoplay() { stopAutoplay(); autoplayInterval = setInterval(function() { goToSlide(currentSlide + 1); }, autoplayDelay); } function stopAutoplay() { if (autoplayInterval) { clearInterval(autoplayInterval); } } // 鼠标悬停时暂停自动播放 $('.achievements-slider-container').on('mouseenter', function() { stopAutoplay(); }); $('.achievements-slider-container').on('mouseleave', function() { // 如果需要自动播放,取消下面这行的注释 // startAutoplay(); }); // 页面加载完成后初始化 setTimeout(function() { updateSlides(); // 如果需要自动播放,取消下面这行的注释 // startAutoplay(); }, 300); // 响应式处理 let resizeTimer; $(window).on('resize', function() { clearTimeout(resizeTimer); resizeTimer = setTimeout(function() { updateSlides(); }, 250); }); console.log('科技成果3D堆叠轮播初始化完成,共', totalSlides, '项成果'); });