-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
241 lines (202 loc) · 6.5 KB
/
Copy pathmain.js
File metadata and controls
241 lines (202 loc) · 6.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
/**
* 字节引力首页交互脚本
*/
// 生成星星
function createStars() {
const container = document.getElementById("stars");
const starCount = 100;
for (let i = 0; i < starCount; i++) {
const star = document.createElement("div");
star.className = "star";
star.style.left = Math.random() * 100 + "%";
star.style.top = Math.random() * 100 + "%";
star.style.width = Math.random() * 3 + 1 + "px";
star.style.height = star.style.width;
star.style.animationDelay = Math.random() * 3 + "s";
star.style.animationDuration = Math.random() * 2 + 2 + "s";
container.appendChild(star);
}
}
// 标题解码动画 - 随机字符闪变后逐字锁定为最终文案
function scrambleLogoText() {
const el = document.querySelector(".logo-text");
if (!el) return;
const lines = el.querySelectorAll(".logo-line");
const chars =
"字节引力天津科技有限公司*广告联盟网络01<>#*+=/\|ABCDEFGHKMNRSTWXYZ";
const frameInterval = 40; // 每帧毫秒数
const lockStagger = 233; // 相邻字符锁定时刻间隔(毫秒),总时长约 1.6s
if (lines.length > 0) {
// 存在 .logo-line 分行元素时,针对每个 span 分别计算并更新,保留 HTML 结构
const lineData = [];
let globalIndex = 0;
lines.forEach((lineEl) => {
const target = lineEl.textContent;
const frames = [];
for (let i = 0; i < target.length; i++) {
const lockAt = target[i].match(/[,。、!?,.!?]/) ? 0 : globalIndex * lockStagger;
frames.push({ char: target[i], lockAt });
globalIndex++;
}
lineData.push({ el: lineEl, target, frames });
});
const start = performance.now();
const timer = setInterval(() => {
const elapsed = performance.now() - start;
let allDone = true;
for (const line of lineData) {
let lineDone = true;
let output = "";
for (const f of line.frames) {
if (elapsed >= f.lockAt) {
output += f.char;
} else {
lineDone = false;
output += chars[Math.floor(Math.random() * chars.length)];
}
}
line.el.textContent = output;
if (!lineDone) {
allDone = false;
}
}
if (allDone) {
clearInterval(timer);
lineData.forEach((line) => {
line.el.textContent = line.target;
});
}
}, frameInterval);
} else {
// 降级处理:无 .logo-line 结构时直接处理整个 el
const target = el.textContent;
const frames = [];
for (let i = 0; i < target.length; i++) {
const lockAt = target[i].match(/[,。、!?,.!?]/) ? 0 : i * lockStagger;
frames.push({ char: target[i], lockAt });
}
const start = performance.now();
const timer = setInterval(() => {
const elapsed = performance.now() - start;
let done = true;
let output = "";
for (const f of frames) {
if (elapsed >= f.lockAt) {
output += f.char;
} else {
done = false;
output += chars[Math.floor(Math.random() * chars.length)];
}
}
el.textContent = output;
if (done) {
clearInterval(timer);
el.textContent = target;
}
}, frameInterval);
}
}
// 滚动动画 - IntersectionObserver
function initScrollAnimations() {
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("visible");
// 如果是统计区域,触发数字动画
if (entry.target.classList.contains("stats-section")) {
animateNumbers();
}
}
});
}, observerOptions);
// 观察所有需要动画的元素
document
.querySelectorAll(".fade-in, .fade-in-left, .fade-in-right")
.forEach((el) => {
observer.observe(el);
});
}
// 数字计数动画
function animateNumbers() {
const numbers = document.querySelectorAll(".stat-number[data-target]");
numbers.forEach((num) => {
if (num.dataset.animated) return;
num.dataset.animated = "true";
const target = parseInt(num.dataset.target);
const suffix = num.dataset.suffix || "";
const duration = 2000;
const step = target / (duration / 16);
let current = 0;
const updateNumber = () => {
current += step;
if (current < target) {
num.textContent = Math.floor(current) + suffix;
requestAnimationFrame(updateNumber);
} else {
num.textContent = target + suffix;
}
};
updateNumber();
});
}
// 3D卡片倾斜效果
function init3DCards() {
const cards = document.querySelectorAll(".product-card");
cards.forEach((card) => {
card.addEventListener("mousemove", (e) => {
const rect = card.getBoundingClientRect();
const x = e.clientX - rect.left;
const y = e.clientY - rect.top;
const centerX = rect.width / 2;
const centerY = rect.height / 2;
const rotateX = (y - centerY) / 40;
const rotateY = (centerX - x) / 40;
card.style.transform = `perspective(1000px) rotateX(${rotateX}deg) rotateY(${rotateY}deg) translateY(-5px)`;
});
card.addEventListener("mouseleave", () => {
card.style.transform =
"perspective(1000px) rotateX(0) rotateY(0) translateY(0)";
});
});
}
// 鼠标光晕效果
function initCursorGlow() {
const glow = document.getElementById("cursorGlow");
if (!glow) return;
let isActive = false;
document.addEventListener("mousemove", (e) => {
if (!isActive) {
isActive = true;
glow.classList.add("active");
}
glow.style.left = e.clientX + "px";
glow.style.top = e.clientY + "px";
});
document.addEventListener("mouseleave", () => {
isActive = false;
glow.classList.remove("active");
});
}
// 视差效果
function initParallax() {
const gravityField = document.querySelector(".gravity-field");
if (!gravityField) return;
document.addEventListener("mousemove", (e) => {
const x = (e.clientX / window.innerWidth - 0.5) * 20;
const y = (e.clientY / window.innerHeight - 0.5) * 20;
gravityField.style.transform = `translate(calc(-50% + ${x}px), calc(-50% + ${y}px))`;
});
}
// 初始化所有效果
document.addEventListener("DOMContentLoaded", () => {
createStars();
scrambleLogoText();
initScrollAnimations();
init3DCards();
initCursorGlow();
initParallax();
});