-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
148 lines (122 loc) · 3.8 KB
/
main.js
File metadata and controls
148 lines (122 loc) · 3.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
/**
* 字节引力首页交互脚本
*/
// 生成星星
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);
}
}
// 滚动动画 - 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();
initScrollAnimations();
init3DCards();
initCursorGlow();
initParallax();
});