Merge remote-tracking branch 'origin/main'
This commit is contained in:
61
backend/public/scripts/docs.js
Normal file
61
backend/public/scripts/docs.js
Normal file
@@ -0,0 +1,61 @@
|
||||
// 平滑滚动功能
|
||||
function setupSmoothScroll() {
|
||||
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
|
||||
anchor.addEventListener('click', function (e) {
|
||||
e.preventDefault();
|
||||
const targetId = this.getAttribute('href');
|
||||
const targetElement = document.querySelector(targetId);
|
||||
|
||||
if (targetElement) {
|
||||
// 使用简单的滚动实现,避免依赖behavior: 'smooth'可能在某些浏览器中不支持
|
||||
window.scrollTo({
|
||||
top: targetElement.offsetTop - 20,
|
||||
behavior: 'auto'
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// 导航高亮功能
|
||||
function setupNavHighlight() {
|
||||
const sections = document.querySelectorAll('section');
|
||||
const navLinks = document.querySelectorAll('nav a');
|
||||
|
||||
window.addEventListener('scroll', () => {
|
||||
let currentSection = '';
|
||||
|
||||
sections.forEach(section => {
|
||||
const sectionTop = section.offsetTop;
|
||||
const sectionHeight = section.clientHeight;
|
||||
if (window.pageYOffset >= (sectionTop - 100)) {
|
||||
currentSection = section.getAttribute('id');
|
||||
}
|
||||
});
|
||||
|
||||
navLinks.forEach(link => {
|
||||
// 移除所有链接的高亮
|
||||
link.style.backgroundColor = '';
|
||||
link.style.color = '';
|
||||
|
||||
// 为当前部分的链接添加高亮
|
||||
if (link.getAttribute('href') === `#${currentSection}`) {
|
||||
link.style.backgroundColor = 'var(--blue-50)';
|
||||
link.style.color = 'var(--primary)';
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
// 页面加载完成后执行初始化
|
||||
function init() {
|
||||
setupSmoothScroll();
|
||||
setupNavHighlight();
|
||||
}
|
||||
|
||||
// 检查DOM是否已经加载完成
|
||||
if (document.readyState === 'loading') {
|
||||
document.addEventListener('DOMContentLoaded', init);
|
||||
} else {
|
||||
init();
|
||||
}
|
||||
Reference in New Issue
Block a user