-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
83 lines (70 loc) · 2.48 KB
/
script.js
File metadata and controls
83 lines (70 loc) · 2.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// ==================== 主题切换 ====================
const themeToggle = document.getElementById('themeToggle');
const html = document.documentElement;
// 检查本地存储的主题
const currentTheme = localStorage.getItem('theme') || 'light';
html.setAttribute('data-theme', currentTheme);
themeToggle.addEventListener('click', () => {
const theme = html.getAttribute('data-theme') === 'light' ? 'dark' : 'light';
html.setAttribute('data-theme', theme);
localStorage.setItem('theme', theme);
});
// ==================== 页面导航 ====================
const navLinks = document.querySelectorAll('.nav-link');
const sections = document.querySelectorAll('.section');
navLinks.forEach(link => {
link.addEventListener('click', (e) => {
e.preventDefault();
const targetId = link.getAttribute('href').substring(1);
// 更新导航状态
navLinks.forEach(l => l.classList.remove('active'));
link.classList.add('active');
// 切换section
sections.forEach(section => {
section.classList.remove('active');
if (section.id === targetId) {
section.classList.add('active');
}
});
});
});
// 滚动到指定section
function scrollToSection(sectionId) {
const targetLink = document.querySelector(`a[href="#${sectionId}"]`);
if (targetLink) {
targetLink.click();
}
}
// ==================== 导航栏滚动效果 ====================
let lastScroll = 0;
const navbar = document.querySelector('.navbar');
window.addEventListener('scroll', () => {
const currentScroll = window.pageYOffset;
if (currentScroll > lastScroll && currentScroll > 100) {
navbar.style.transform = 'translateY(-100%)';
} else {
navbar.style.transform = 'translateY(0)';
}
lastScroll = currentScroll;
});
// ==================== 页面初始化 ====================
window.addEventListener('load', () => {
console.log('🎉 欢迎来到 Bless Tien 的个人网站!');
console.log('💼 智能硬件 / 智能家居方向产品经理');
console.log('🔬 跨界搞产品的科研博士');
});
// 添加平滑滚动动画样式
const style = document.createElement('style');
style.textContent = `
@keyframes fadeIn {
from {
opacity: 0;
transform: translateY(20px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
`;
document.head.appendChild(style);