-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
364 lines (316 loc) · 10.5 KB
/
script.js
File metadata and controls
364 lines (316 loc) · 10.5 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// Mobile Navigation Toggle
const navToggle = document.querySelector('.nav-toggle');
const navMenu = document.querySelector('.nav-menu');
navToggle.addEventListener('click', () => {
navMenu.classList.toggle('active');
// Animate hamburger menu
const spans = navToggle.querySelectorAll('span');
spans[0].style.transform = navMenu.classList.contains('active') ? 'rotate(45deg) translateY(8px)' : '';
spans[1].style.opacity = navMenu.classList.contains('active') ? '0' : '1';
spans[2].style.transform = navMenu.classList.contains('active') ? 'rotate(-45deg) translateY(-8px)' : '';
});
// Close mobile menu when clicking on a link
document.querySelectorAll('.nav-menu a').forEach(link => {
link.addEventListener('click', () => {
navMenu.classList.remove('active');
const spans = navToggle.querySelectorAll('span');
spans[0].style.transform = '';
spans[1].style.opacity = '1';
spans[2].style.transform = '';
});
});
// Smooth scroll for navigation 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'
});
}
});
});
// Active navigation highlighting
window.addEventListener('scroll', () => {
let current = '';
const sections = document.querySelectorAll('section[id]');
sections.forEach(section => {
const sectionTop = section.offsetTop;
const sectionHeight = section.clientHeight;
if (scrollY >= (sectionTop - 100)) {
current = section.getAttribute('id');
}
});
document.querySelectorAll('.nav-menu a').forEach(link => {
link.classList.remove('active');
if (link.getAttribute('href').slice(1) === current) {
link.classList.add('active');
}
});
});
// Contact Form Handling
const contactForm = document.getElementById('contactForm');
if (contactForm) {
contactForm.addEventListener('submit', async function(e) {
e.preventDefault();
const formData = new FormData(this);
const formObject = {};
formData.forEach((value, key) => {
formObject[key] = value;
});
// Show loading state
const submitButton = this.querySelector('button[type="submit"]');
const originalText = submitButton.textContent;
submitButton.textContent = '发送中...';
submitButton.disabled = true;
try {
// Here you would normally send the data to your server
// For demo purposes, we'll simulate a successful submission
await new Promise(resolve => setTimeout(resolve, 1500));
// Show success message
showNotification('消息发送成功!我会尽快回复您。', 'success');
// Reset form
this.reset();
} catch (error) {
// Show error message
showNotification('发送失败,请稍后重试。', 'error');
} finally {
// Reset button
submitButton.textContent = originalText;
submitButton.disabled = false;
}
});
}
// Notification System
function showNotification(message, type = 'info') {
// Remove existing notifications
const existingNotification = document.querySelector('.notification');
if (existingNotification) {
existingNotification.remove();
}
// Create notification element
const notification = document.createElement('div');
notification.className = `notification notification-${type}`;
notification.textContent = message;
// Add styles
notification.style.cssText = `
position: fixed;
top: 20px;
right: 20px;
padding: 1rem 1.5rem;
background-color: ${type === 'success' ? '#10b981' : type === 'error' ? '#ef4444' : '#3b82f6'};
color: white;
border-radius: 0.5rem;
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
z-index: 10000;
transform: translateX(100%);
transition: transform 0.3s ease;
max-width: 300px;
`;
// Add to page
document.body.appendChild(notification);
// Animate in
setTimeout(() => {
notification.style.transform = 'translateX(0)';
}, 100);
// Remove after 5 seconds
setTimeout(() => {
notification.style.transform = 'translateX(100%)';
setTimeout(() => {
notification.remove();
}, 300);
}, 5000);
}
// Typing Animation for Hero Title
function typeWriter(element, text, speed = 100) {
let i = 0;
element.textContent = '';
function type() {
if (i < text.length) {
element.textContent += text.charAt(i);
i++;
setTimeout(type, speed);
}
}
type();
}
// Initialize typing animation when page loads
document.addEventListener('DOMContentLoaded', () => {
const heroTitle = document.querySelector('.hero-title');
if (heroTitle) {
// Only animate on larger screens
if (window.innerWidth > 768) {
const originalText = heroTitle.textContent;
typeWriter(heroTitle, originalText, 50);
}
}
// Add fade-in animation to elements
const fadeElements = document.querySelectorAll('.project-card, .blog-card, .about-content > div, .contact-content > div');
fadeElements.forEach((element, index) => {
element.style.opacity = '0';
element.style.transform = 'translateY(20px)';
element.style.transition = 'opacity 0.6s ease, transform 0.6s ease';
// Stagger animation
setTimeout(() => {
element.style.opacity = '1';
element.style.transform = 'translateY(0)';
}, 200 + (index * 100));
});
});
// Scroll animations
const observerOptions = {
threshold: 0.1,
rootMargin: '0px 0px -50px 0px'
};
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
entry.target.classList.add('animate-in');
}
});
}, observerOptions);
// Observe all sections for animation
document.addEventListener('DOMContentLoaded', () => {
const sections = document.querySelectorAll('.section');
sections.forEach(section => {
observer.observe(section);
});
});
// Add CSS for animations
const style = document.createElement('style');
style.textContent = `
.animate-in {
animation: fadeInUp 0.6s ease forwards;
}
@keyframes fadeInUp {
from {
opacity: 0;
transform: translateY(30px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.nav-menu a.active {
color: var(--primary-color);
position: relative;
}
.nav-menu a.active::after {
content: '';
position: absolute;
bottom: -5px;
left: 0;
width: 100%;
height: 2px;
background-color: var(--primary-color);
}
@media (prefers-reduced-motion: reduce) {
.animate-in {
animation: none;
}
}
`;
document.head.appendChild(style);
// Theme Toggle (Optional Feature)
function createThemeToggle() {
const themeToggle = document.createElement('button');
themeToggle.innerHTML = '<i class="fas fa-moon"></i>';
themeToggle.className = 'theme-toggle';
themeToggle.style.cssText = `
position: fixed;
bottom: 20px;
right: 20px;
width: 50px;
height: 50px;
border-radius: 50%;
background-color: var(--primary-color);
color: white;
border: none;
cursor: pointer;
box-shadow: var(--shadow-lg);
transition: var(--transition);
z-index: 1000;
`;
themeToggle.addEventListener('click', () => {
document.body.classList.toggle('dark-theme');
const isDark = document.body.classList.contains('dark-theme');
themeToggle.innerHTML = isDark ? '<i class="fas fa-sun"></i>' : '<i class="fas fa-moon"></i>';
// Save theme preference
localStorage.setItem('theme', isDark ? 'dark' : 'light');
});
// Load saved theme
const savedTheme = localStorage.getItem('theme');
if (savedTheme === 'dark') {
document.body.classList.add('dark-theme');
themeToggle.innerHTML = '<i class="fas fa-sun"></i>';
}
document.body.appendChild(themeToggle);
}
// Add dark theme styles
function addDarkThemeStyles() {
const darkThemeStyles = document.createElement('style');
darkThemeStyles.textContent = `
.dark-theme {
--primary-color: #3b82f6;
--primary-dark: #2563eb;
--text-color: #f1f5f9;
--text-light: #94a3b8;
--bg-color: #0f172a;
--bg-alt: #1e293b;
--border-color: #334155;
}
.dark-theme .header {
background-color: rgba(15, 23, 42, 0.95);
}
.dark-theme .project-card,
.dark-theme .blog-card,
.dark-theme .contact-form {
background-color: var(--bg-alt);
}
.dark-theme .skill-tag,
.dark-theme .blog-tag {
background-color: var(--bg-color);
}
`;
document.head.appendChild(darkThemeStyles);
}
// Initialize theme features
document.addEventListener('DOMContentLoaded', () => {
addDarkThemeStyles();
createThemeToggle();
});
// Performance optimization: Lazy load images
document.addEventListener('DOMContentLoaded', () => {
const images = document.querySelectorAll('img[data-src]');
const imageObserver = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const img = entry.target;
img.src = img.dataset.src;
img.removeAttribute('data-src');
imageObserver.unobserve(img);
}
});
});
images.forEach(img => imageObserver.observe(img));
});
// Utility functions
function debounce(func, wait) {
let timeout;
return function executedFunction(...args) {
const later = () => {
clearTimeout(timeout);
func(...args);
};
clearTimeout(timeout);
timeout = setTimeout(later, wait);
};
}
// Optimize scroll performance
const optimizedScroll = debounce(() => {
// Scroll-related functions that don't need to run on every pixel
}, 10);
window.addEventListener('scroll', optimizedScroll);