feat(website): 优化新闻轮播功能并更新导航链接

This commit is contained in:
ylweng
2025-08-21 03:22:59 +08:00
parent 5ff7d38904
commit 451bab4a96
6 changed files with 960 additions and 19 deletions

View File

@@ -28,20 +28,65 @@ document.addEventListener('DOMContentLoaded', function() {
});
// 新闻轮播
const newsItems = document.querySelectorAll('#news .card');
const newsContainer = document.querySelector('#news .row');
const newsItems = document.querySelectorAll('#news .col-md-4');
if (newsItems.length > 0) {
// 创建轮播指示器
const indicatorsContainer = document.createElement('div');
indicatorsContainer.className = 'carousel-indicators d-flex justify-content-center mt-3';
// 为每个新闻项创建指示器
newsItems.forEach((item, index) => {
const indicator = document.createElement('button');
indicator.type = 'button';
indicator.className = index === 0 ? 'active' : '';
indicator.setAttribute('data-bs-target', '#news');
indicator.setAttribute('data-bs-slide-to', index.toString());
indicator.addEventListener('click', () => {
showNews(index);
});
indicatorsContainer.appendChild(indicator);
});
// 将指示器添加到新闻区域
newsContainer.parentNode.insertBefore(indicatorsContainer, newsContainer.nextSibling);
let currentIndex = 0;
function showNextNews() {
newsItems[currentIndex].classList.remove('active');
currentIndex = (currentIndex + 1) % newsItems.length;
newsItems[currentIndex].classList.add('active');
function showNews(index) {
// 隐藏所有新闻项
newsItems.forEach(item => {
item.classList.remove('d-block');
item.classList.add('d-none');
});
// 显示指定新闻项
newsItems[index].classList.remove('d-none');
newsItems[index].classList.add('d-block');
// 更新指示器
const indicators = indicatorsContainer.querySelectorAll('button');
indicators.forEach((indicator, i) => {
if (i === index) {
indicator.classList.add('active');
} else {
indicator.classList.remove('active');
}
});
currentIndex = index;
}
// 初始化第一个新闻项
newsItems[0].classList.add('active');
showNews(0);
// 每5秒自动轮播
setInterval(showNextNews, 5000);
setInterval(() => {
const nextIndex = (currentIndex + 1) % newsItems.length;
showNews(nextIndex);
}, 5000);
}
});