-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
302 lines (267 loc) · 9.87 KB
/
script.js
File metadata and controls
302 lines (267 loc) · 9.87 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
// ===================================
// Smooth Scrolling Navigation
// ===================================
document.querySelectorAll('a[href^="#"]').forEach(anchor => {
anchor.addEventListener('click', function (e) {
e.preventDefault();
const target = document.querySelector(this.getAttribute('href'));
if (target) {
const navbarHeight = document.querySelector('.navbar').offsetHeight;
const targetPosition = target.offsetTop - navbarHeight;
window.scrollTo({
top: targetPosition,
behavior: 'smooth'
});
// Close mobile menu if open
const navMenu = document.getElementById('nav-menu');
if (navMenu.classList.contains('active')) {
navMenu.classList.remove('active');
toggleMenuIcon(false);
}
}
});
});
// ===================================
// Mobile Menu Toggle
// ===================================
const mobileMenuToggle = document.getElementById('mobile-menu-toggle');
const navMenu = document.getElementById('nav-menu');
if (mobileMenuToggle) {
mobileMenuToggle.addEventListener('click', () => {
navMenu.classList.toggle('active');
toggleMenuIcon(navMenu.classList.contains('active'));
});
}
function toggleMenuIcon(isOpen) {
const spans = mobileMenuToggle.querySelectorAll('span');
if (isOpen) {
spans[0].style.transform = 'rotate(45deg) translateY(8px)';
spans[1].style.opacity = '0';
spans[2].style.transform = 'rotate(-45deg) translateY(-8px)';
} else {
spans[0].style.transform = 'none';
spans[1].style.opacity = '1';
spans[2].style.transform = 'none';
}
}
// ===================================
// Navbar Scroll Effect
// ===================================
let lastScrollTop = 0;
const navbar = document.getElementById('navbar');
window.addEventListener('scroll', () => {
const scrollTop = window.pageYOffset || document.documentElement.scrollTop;
// Add shadow on scroll
if (scrollTop > 50) {
navbar.style.boxShadow = '0 4px 6px -1px rgba(0, 0, 0, 0.3)';
} else {
navbar.style.boxShadow = 'none';
}
lastScrollTop = scrollTop;
});
// ===================================
// Intersection Observer for Animations
// ===================================
const observerOptions = {
threshold: 0.05,
rootMargin: '0px 0px 100px 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 cards and timeline items
const animateElements = document.querySelectorAll(
'.advantage-card, .timeline-item, .education-card, .cert-card, .contact-item'
);
animateElements.forEach((el, index) => {
el.style.opacity = '0';
el.style.transform = 'translateY(30px)';
// Use modulo to reset delay every 3 items, preventing long delays for bottom sections
// Max delay will be 0.2s
const delay = (index % 3) * 0.1;
el.style.transition = `opacity 0.5s ease-out ${delay}s, transform 0.5s ease-out ${delay}s`;
observer.observe(el);
});
// ===================================
// Dynamic Year in Footer
// ===================================
const currentYearElement = document.getElementById('current-year');
if (currentYearElement) {
currentYearElement.textContent = new Date().getFullYear();
}
// ===================================
// Active Navigation Link Highlighting
// ===================================
const sections = document.querySelectorAll('section[id]');
const navLinks = document.querySelectorAll('.nav-link');
window.addEventListener('scroll', () => {
let current = '';
const scrollPosition = window.pageYOffset + 100;
sections.forEach(section => {
const sectionTop = section.offsetTop;
const sectionHeight = section.clientHeight;
if (scrollPosition >= sectionTop && scrollPosition < sectionTop + sectionHeight) {
current = section.getAttribute('id');
}
});
navLinks.forEach(link => {
link.classList.remove('active');
if (link.getAttribute('href') === `#${current}`) {
link.classList.add('active');
}
});
});
// ===================================
// Parallax Effect for Hero Background
// ===================================
const heroBackground = document.querySelector('.hero-background');
window.addEventListener('scroll', () => {
const scrolled = window.pageYOffset;
if (heroBackground && scrolled < window.innerHeight) {
heroBackground.style.transform = `translateY(${scrolled * 0.5}px)`;
heroBackground.style.opacity = 1 - (scrolled / window.innerHeight);
}
});
// ===================================
// Stats Counter Animation
// ===================================
function animateCounter(element, target, duration = 2000) {
const start = 0;
const increment = target / (duration / 16);
let current = start;
const timer = setInterval(() => {
current += increment;
if (current >= target) {
element.textContent = target;
clearInterval(timer);
} else {
element.textContent = Math.floor(current);
}
}, 16);
}
// Trigger counter animation when stats section is visible
const statsObserver = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const statNumbers = entry.target.querySelectorAll('.stat-number');
statNumbers.forEach(stat => {
const text = stat.textContent;
// Only animate if it's a number
if (!isNaN(text)) {
animateCounter(stat, parseInt(text));
}
});
statsObserver.unobserve(entry.target);
}
});
}, { threshold: 0.5 });
const heroStats = document.querySelector('.hero-stats');
if (heroStats) {
statsObserver.observe(heroStats);
}
// ===================================
// Copy Email on Click
// ===================================
const emailLinks = document.querySelectorAll('a[href^="mailto:"]');
emailLinks.forEach(link => {
link.addEventListener('click', (e) => {
const email = link.textContent;
if (navigator.clipboard) {
navigator.clipboard.writeText(email).then(() => {
// Create temporary tooltip
const tooltip = document.createElement('span');
tooltip.textContent = '已复制!';
tooltip.style.cssText = `
position: absolute;
background: #10b981;
color: white;
padding: 4px 8px;
border-radius: 4px;
font-size: 12px;
margin-left: 10px;
animation: fadeInUp 0.3s ease-out;
`;
link.parentElement.style.position = 'relative';
link.parentElement.appendChild(tooltip);
setTimeout(() => {
tooltip.remove();
}, 2000);
});
}
});
});
// ===================================
// Scroll to Top Button (Optional)
// ===================================
function createScrollToTopButton() {
const button = document.createElement('button');
button.innerHTML = '↑';
button.className = 'scroll-to-top';
button.style.cssText = `
position: fixed;
bottom: 30px;
right: 30px;
width: 50px;
height: 50px;
border-radius: 50%;
background: linear-gradient(135deg, #6366f1 0%, #8b5cf6 100%);
color: white;
border: none;
font-size: 24px;
cursor: pointer;
opacity: 0;
visibility: hidden;
transition: all 0.3s ease;
z-index: 999;
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.3);
`;
button.addEventListener('click', () => {
window.scrollTo({
top: 0,
behavior: 'smooth'
});
});
window.addEventListener('scroll', () => {
if (window.pageYOffset > 500) {
button.style.opacity = '1';
button.style.visibility = 'visible';
} else {
button.style.opacity = '0';
button.style.visibility = 'hidden';
}
});
document.body.appendChild(button);
}
// Initialize scroll to top button
createScrollToTopButton();
// ===================================
// Performance: Lazy Load Images
// ===================================
if ('IntersectionObserver' in window) {
const imageObserver = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
const img = entry.target;
if (img.dataset.src) {
img.src = img.dataset.src;
img.removeAttribute('data-src');
}
imageObserver.unobserve(img);
}
});
});
document.querySelectorAll('img[data-src]').forEach(img => {
imageObserver.observe(img);
});
}
// ===================================
// Console Easter Egg
// ===================================
console.log('%c👋 Hello there!', 'font-size: 20px; font-weight: bold; color: #6366f1;');
console.log('%c感谢查看我的个人网站!如果您对我的经历感兴趣,欢迎联系我。', 'font-size: 14px; color: #94a3b8;');
console.log('%c📧 longyun.bao@foxmail.com', 'font-size: 12px; color: #06b6d4;');