-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
234 lines (199 loc) · 7.8 KB
/
script.js
File metadata and controls
234 lines (199 loc) · 7.8 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Theme Management
class ThemeManager {
constructor() {
this.themeToggle = document.getElementById('theme-toggle');
this.themeIcon = document.getElementById('theme-icon');
this.currentTheme = this.getInitialTheme();
this.init();
}
getInitialTheme() {
// Check localStorage first
const savedTheme = localStorage.getItem('theme');
if (savedTheme) {
return savedTheme;
}
// Check system preference
const prefersDark = window.matchMedia('(prefers-color-scheme: dark)').matches;
return prefersDark ? 'dark' : 'light';
}
init() {
// Set initial theme
this.applyTheme(this.currentTheme);
// Add click event listener
this.themeToggle.addEventListener('click', () => {
this.toggleTheme();
});
// Listen for system theme changes
window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', (e) => {
// Only auto-switch if user hasn't manually set a preference
if (!localStorage.getItem('theme')) {
this.currentTheme = e.matches ? 'dark' : 'light';
this.applyTheme(this.currentTheme);
}
});
// Add smooth scroll behavior for any internal links
this.initSmoothScroll();
// Add intersection observer for animations
this.initAnimations();
}
toggleTheme() {
this.currentTheme = this.currentTheme === 'light' ? 'dark' : 'light';
this.applyTheme(this.currentTheme);
// Save preference to localStorage
localStorage.setItem('theme', this.currentTheme);
}
applyTheme(theme) {
document.documentElement.setAttribute('data-theme', theme);
// Update icon
if (theme === 'dark') {
this.themeIcon.textContent = 'dark_mode';
this.themeToggle.setAttribute('aria-label', '切换到亮色模式');
} else {
this.themeIcon.textContent = 'light_mode';
this.themeToggle.setAttribute('aria-label', '切换到暗色模式');
}
}
initSmoothScroll() {
// Add smooth scrolling for any anchor links
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute('href'));
if (target) {
target.scrollIntoView({
behavior: 'smooth',
block: 'start'
});
}
});
});
}
initAnimations() {
// Create intersection observer for fade-in animations
const observerOptions = {
threshold: 0.1,
rootMargin: '0px 0px -50px 0px'
};
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.style.opacity = '1';
entry.target.style.transform = 'translateY(0)';
}
});
}, observerOptions);
// Observe all sections
document.querySelectorAll('section').forEach(section => {
section.style.opacity = '0';
section.style.transform = 'translateY(20px)';
section.style.transition = 'opacity 0.6s ease, transform 0.6s ease';
observer.observe(section);
});
// Observe hero section separately with different timing
const heroSection = document.querySelector('.hero-section');
if (heroSection) {
heroSection.style.opacity = '0';
heroSection.style.transform = 'translateY(30px)';
heroSection.style.transition = 'opacity 0.8s ease, transform 0.8s ease';
observer.observe(heroSection);
}
}
}
// Utility Functions
class Utils {
static addHoverEffects() {
// Add subtle hover effects to cards
const cards = document.querySelectorAll('.project-card, .contact-item');
cards.forEach(card => {
card.addEventListener('mouseenter', function() {
this.style.transform = 'translateY(-5px) scale(1.02)';
});
card.addEventListener('mouseleave', function() {
this.style.transform = 'translateY(0) scale(1)';
});
});
}
static initTooltips() {
// Add tooltips for external links
const externalLinks = document.querySelectorAll('a[target="_blank"]');
externalLinks.forEach(link => {
link.addEventListener('mouseenter', function() {
const tooltip = document.createElement('div');
tooltip.className = 'tooltip';
tooltip.textContent = '点击访问外部链接';
tooltip.style.position = 'absolute';
tooltip.style.background = 'rgba(0, 0, 0, 0.8)';
tooltip.style.color = 'white';
tooltip.style.padding = '0.5rem';
tooltip.style.borderRadius = '0.5rem';
tooltip.style.fontSize = '0.8rem';
tooltip.style.whiteSpace = 'nowrap';
tooltip.style.zIndex = '1000';
tooltip.style.pointerEvents = 'none';
tooltip.style.opacity = '0';
tooltip.style.transition = 'opacity 0.3s ease';
document.body.appendChild(tooltip);
this.addEventListener('mousemove', function(e) {
tooltip.style.left = e.pageX + 10 + 'px';
tooltip.style.top = e.pageY - 40 + 'px';
tooltip.style.opacity = '1';
});
this.addEventListener('mouseleave', function() {
document.body.removeChild(tooltip);
});
});
});
}
}
// Performance optimization - lazy loading for animations
class PerformanceOptimizer {
static init() {
// Reduce animations on devices with reduced motion preference
if (window.matchMedia('(prefers-reduced-motion: reduce)').matches) {
document.documentElement.style.setProperty('--animation-duration', '0s');
}
// Optimize for mobile devices
if (window.innerWidth < 768) {
document.body.classList.add('mobile-optimized');
}
}
}
// Initialize everything when DOM is loaded
document.addEventListener('DOMContentLoaded', function() {
// Initialize theme management
new ThemeManager();
// Initialize utility functions
Utils.addHoverEffects();
Utils.initTooltips();
// Initialize performance optimizations
PerformanceOptimizer.init();
// Add loading animation
document.body.style.opacity = '0';
document.body.style.transition = 'opacity 0.5s ease';
setTimeout(() => {
document.body.style.opacity = '1';
}, 100);
});
// Handle window resize
window.addEventListener('resize', function() {
// Recalculate mobile optimizations
if (window.innerWidth < 768) {
document.body.classList.add('mobile-optimized');
} else {
document.body.classList.remove('mobile-optimized');
}
});
// Add keyboard navigation support
document.addEventListener('keydown', function(e) {
// Toggle theme with Ctrl/Cmd + D
if ((e.ctrlKey || e.metaKey) && e.key === 'd') {
e.preventDefault();
document.getElementById('theme-toggle').click();
}
});
// Export for potential future use
window.PersonalHomepage = {
ThemeManager,
Utils,
PerformanceOptimizer
};