-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathanimation.js
356 lines (300 loc) · 12.5 KB
/
animation.js
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
// 在文件开头添加字体加载检测
document.fonts.ready.then(() => {
const logo = document.querySelector('.logo');
const langSwitch = document.querySelector('.language-switch');
logo.classList.add('font-loaded');
langSwitch.style.opacity = '1';
});
const canvas = document.getElementById('bgCanvas');
const ctx = canvas.getContext('2d');
// 设置canvas尺寸
function setCanvasSize() {
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
}
setCanvasSize();
window.addEventListener('resize', setCanvasSize);
// 粒子类
class Particle {
constructor() {
this.reset();
}
reset() {
this.x = Math.random() * canvas.width;
this.y = Math.random() * canvas.height;
this.size = Math.random() * 2 + 0.5;
this.speedX = Math.random() * 0.5 - 0.25;
this.speedY = Math.random() * 0.5 - 0.25;
this.life = Math.random() * 100 + 100;
this.opacity = Math.random() * 0.35 + 0.25;
this.originalOpacity = this.opacity;
}
update() {
this.x += this.speedX;
this.y += this.speedY;
this.life--;
if (this.life <= 0 ||
this.x < 0 || this.x > canvas.width ||
this.y < 0 || this.y > canvas.height) {
this.reset();
}
}
draw() {
ctx.beginPath();
ctx.arc(this.x, this.y, this.size, 0, Math.PI * 2);
ctx.fillStyle = `rgba(255, 255, 255, ${this.opacity})`;
ctx.fill();
}
}
// 创建粒子数组
const particles = [];
const particleCount = 75;
for (let i = 0; i < particleCount; i++) {
particles.push(new Particle());
}
// 连线函数
function drawLines(fadeOpacity = 1) {
for (let i = 0; i < particles.length; i++) {
for (let j = i + 1; j < particles.length; j++) {
const dx = particles[i].x - particles[j].x;
const dy = particles[i].y - particles[j].y;
const distance = Math.sqrt(dx * dx + dy * dy);
if (distance < 80) {
ctx.beginPath();
ctx.strokeStyle = `rgba(255, 255, 255, ${0.1 * (1 - distance / 80) * fadeOpacity})`;
ctx.lineWidth = 0.5;
ctx.moveTo(particles[i].x, particles[i].y);
ctx.lineTo(particles[j].x, particles[j].y);
ctx.stroke();
}
}
}
}
// 动画循环
let fadeOutStartTime = null;
const fadeOutDuration = 5000; // 5秒
function animate(timestamp) {
if (!fadeOutStartTime) fadeOutStartTime = timestamp;
const fadeProgress = (timestamp - fadeOutStartTime) / fadeOutDuration;
ctx.fillStyle = 'rgba(10, 10, 10, 0.1)';
ctx.fillRect(0, 0, canvas.width, canvas.height);
if (fadeProgress <= 1) {
const opacity = 1 - fadeProgress;
particles.forEach(particle => {
particle.opacity = Math.min(particle.originalOpacity * opacity, 0.5);
particle.update();
particle.draw();
});
drawLines(opacity);
}
requestAnimationFrame(animate);
}
animate();
// 动画控制
const mainTitle = document.querySelector('.main-title');
const floatingChars = document.querySelectorAll('.floating-characters span');
const codeContainers = document.querySelectorAll('.code-container');
const descriptionContainers = document.querySelectorAll('.description-container');
const hero = document.querySelector('.hero');
// 初始化所有元素为隐藏状态
mainTitle.style.opacity = '0';
floatingChars.forEach(char => char.style.opacity = '0');
codeContainers.forEach(container => container.style.opacity = '0');
descriptionContainers.forEach(container => container.style.opacity = '0');
// 添加组织部分的动画观察
const orgContent = document.querySelector('.org-content');
const memberCards = document.querySelectorAll('.member-card');
// 创建观察器
const observer = new IntersectionObserver((entries) => {
entries.forEach(entry => {
if (entry.isIntersecting) {
if (entry.target === mainTitle) {
mainTitle.style.animation = 'fadeIn 2s ease-out forwards';
// 触发浮动字符动画
floatingChars.forEach((char, index) => {
char.style.animation = `floatIn 1.5s ease-out forwards ${0.5 + index * 0.3}s`;
});
} else if (entry.target === orgContent) {
// 触发组织内容的动画
const title = orgContent.querySelector('.org-title');
const description = orgContent.querySelector('.org-description');
const cards = orgContent.querySelectorAll('.member-card');
// 标题动画
title.style.animationPlayState = 'running';
// 描述动画,延迟0.3秒
setTimeout(() => {
description.style.animationPlayState = 'running';
}, 300);
// 成员卡片动画,依次延迟显示
cards.forEach((card, index) => {
setTimeout(() => {
card.style.animationPlayState = 'running';
}, 600 + index * 200); // 从0.6秒开始,每个卡片间隔0.2秒
});
} else if (entry.target.classList.contains('code-container')) {
// 代码容器动画
entry.target.style.animation = 'codeAppear 1.2s cubic-bezier(0.23, 1, 0.32, 1) forwards';
// 获取相邻的描述容器
const descContainer = entry.target.closest('.code-section').querySelector('.description-container');
if (descContainer) {
descContainer.style.animation = 'slideLeft 1s ease-out forwards';
// 触发标签动画
const tags = descContainer.querySelectorAll('.feature-tags span');
tags.forEach((tag, index) => {
tag.style.animation = `tagReveal 0.6s cubic-bezier(0.23, 1, 0.32, 1) forwards ${0.8 + index * 0.2}s`;
});
// 触发 GitHub 按钮动画
const githubLink = descContainer.querySelector('.github-link');
if (githubLink) {
githubLink.style.animation = 'buttonFadeIn 0.8s cubic-bezier(0.23, 1, 0.32, 1) forwards 1.4s';
}
}
} else if (entry.target.classList.contains('mission-content')) {
// 触发使命部分的动画
const title = entry.target.querySelector('.mission-title');
const subtitle = entry.target.querySelector('.mission-subtitle');
const line = entry.target.querySelector('.mission-line');
const cards = entry.target.querySelectorAll('.mission-card');
// 标题和副标题动画
title.style.animationPlayState = 'running';
subtitle.style.animationPlayState = 'running';
line.style.animationPlayState = 'running';
// 卡片动画,依次延迟显示
cards.forEach((card, index) => {
setTimeout(() => {
card.style.animationPlayState = 'running';
}, 800 + index * 200);
});
} else if (entry.target.classList.contains('contact-content')) {
// 触发联系方式部分的动画
const title = entry.target.querySelector('.contact-title');
const subtitle = entry.target.querySelector('.contact-subtitle');
const line = entry.target.querySelector('.contact-line');
const cards = entry.target.querySelectorAll('.contact-card');
const footer = entry.target.querySelector('.footer');
// 标题和副标题动画
title.style.animationPlayState = 'running';
subtitle.style.animationPlayState = 'running';
line.style.animationPlayState = 'running';
// 卡片动画,依次延迟显示
cards.forEach((card, index) => {
setTimeout(() => {
card.style.animationPlayState = 'running';
}, 800 + index * 200);
});
// 页脚动画
footer.style.animationPlayState = 'running';
}
observer.unobserve(entry.target);
}
});
}, {
threshold: 0.1
});
// 观察所有需要动画的元素
observer.observe(mainTitle);
observer.observe(orgContent);
codeContainers.forEach(container => observer.observe(container));
const missionContent = document.querySelector('.mission-content');
observer.observe(missionContent);
const contactContent = document.querySelector('.contact-content');
observer.observe(contactContent);
// 修改轮播逻辑
const slider = {
track: document.querySelector('.members-track'),
prevBtn: document.querySelector('.nav-arrow.prev'),
nextBtn: document.querySelector('.nav-arrow.next'),
dots: document.querySelector('.slider-dots'),
isDragging: false,
startPos: 0,
currentTranslate: 0,
prevTranslate: 0,
init() {
if (!this.track) return;
this.bindEvents();
// 计算最大拖动距离
const trackWidth = this.track.scrollWidth;
const containerWidth = this.track.parentElement.clientWidth;
this.maxTranslate = containerWidth * 0.1; // 右边保持10%的余量
this.minTranslate = -(trackWidth - containerWidth + containerWidth * 0.8); // 左边增加到80%的容器宽度
// 移除轮播点和导航按钮
this.dots.style.display = 'none';
this.prevBtn.style.display = 'none';
this.nextBtn.style.display = 'none';
},
bindEvents() {
// 添加拖动事件
this.track.addEventListener('mousedown', this.startDragging.bind(this));
this.track.addEventListener('touchstart', this.startDragging.bind(this));
window.addEventListener('mousemove', this.drag.bind(this));
window.addEventListener('touchmove', this.drag.bind(this));
window.addEventListener('mouseup', this.stopDragging.bind(this));
window.addEventListener('touchend', this.stopDragging.bind(this));
// 防止拖动时选中文本
this.track.addEventListener('selectstart', (e) => e.preventDefault());
},
startDragging(e) {
this.isDragging = true;
this.startPos = this.getPositionX(e);
this.track.style.cursor = 'grabbing';
this.track.classList.add('dragging');
},
drag(e) {
if (!this.isDragging) return;
const currentPosition = this.getPositionX(e);
const diff = currentPosition - this.startPos;
const walk = diff * 1.0;
// 添加边界限制
const nextTranslate = this.prevTranslate + walk;
this.currentTranslate = Math.max(
this.minTranslate,
Math.min(this.maxTranslate, nextTranslate)
);
this.setSliderPosition();
},
stopDragging() {
if (!this.isDragging) return;
this.isDragging = false;
this.track.style.cursor = 'grab';
this.track.classList.remove('dragging');
this.track.classList.add('snap');
// 添加弹性效果
requestAnimationFrame(() => {
this.prevTranslate = this.currentTranslate;
// 300ms 后移除 snap 类
setTimeout(() => {
this.track.classList.remove('snap');
}, 300);
});
},
getPositionX(e) {
return e.type.includes('mouse') ? e.pageX : e.touches[0].clientX;
},
setSliderPosition() {
this.track.style.transform = `translateX(${this.currentTranslate}px)`;
}
};
// 初始化轮询
slider.init();
// 修改滚动处理逻辑
let lastScrollY = window.scrollY;
let ticking = false;
window.addEventListener('scroll', () => {
if (!ticking) {
window.requestAnimationFrame(() => {
const nav = document.querySelector('nav');
const langSwitch = document.querySelector('.language-switch');
// 向下滚动超过100px时隐藏
if (window.scrollY > lastScrollY && window.scrollY > 100) {
nav.classList.add('hidden');
langSwitch.classList.add('hidden');
} else {
nav.classList.remove('hidden');
langSwitch.classList.remove('hidden');
}
lastScrollY = window.scrollY;
ticking = false;
});
ticking = true;
}
});