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 |
x1
|
|
import { useEffect, useRef, useState } from "preact/hooks";
interface CursorState {
x: number;
y: number;
isHovering: boolean;
isClicking: boolean;
cursorType:
| "default"
| "hover"
| "link"
| "button"
| "text"
| "disabled"
| "loading";
}
export default function CustomCursor() {
const cursorRef = useRef<HTMLDivElement>(null);
const followerRef = useRef<HTMLDivElement>(null);
const [cursorState, setCursorState] = useState<CursorState>({
x: 0,
y: 0,
isHovering: false,
isClicking: false,
cursorType: "default",
});
useEffect(() => {
let animationFrameId: number;
let mouseX = 0;
let mouseY = 0;
let currentX = 0;
let currentY = 0;
let followerX = 0;
let followerY = 0;
// 鼠标移动处理
const handleMouseMove = (e: MouseEvent) => {
mouseX = e.clientX;
mouseY = e.clientY;
};
// 鼠标按下处理
const handleMouseDown = () => {
setCursorState((prev) => ({ ...prev, isClicking: true }));
document.body.classList.add("cursor-click");
};
// 鼠标松开处理
const handleMouseUp = () => {
setCursorState((prev) => ({ ...prev, isClicking: false }));
document.body.classList.remove("cursor-click");
};
// 鼠标进入元素处理
const handleMouseEnter = (e: Event) => {
const target = e.target as HTMLElement;
if (target.matches('a, [role="link"]')) {
setCursorState((prev) => ({
...prev,
cursorType: "link",
isHovering: true,
}));
document.body.classList.add("cursor-link");
} else if (target.matches('button, [role="button"], .btn, .clickable')) {
setCursorState((prev) => ({
...prev,
cursorType: "button",
isHovering: true,
}));
document.body.classList.add("cursor-button");
} else if (target.matches("input, textarea, [contenteditable]")) {
setCursorState((prev) => ({
...prev,
cursorType: "text",
isHovering: true,
}));
document.body.classList.add("cursor-text");
} else if (target.matches("[disabled], .disabled")) {
setCursorState((prev) => ({
...prev,
cursorType: "disabled",
isHovering: true,
}));
document.body.classList.add("cursor-disabled");
} else if (target.matches(".loading, .cursor-loading")) {
setCursorState((prev) => ({
...prev,
cursorType: "loading",
isHovering: true,
}));
document.body.classList.add("cursor-loading");
} else if (target.matches(".hover-target, .card, .nav-link")) {
setCursorState((prev) => ({
...prev,
cursorType: "hover",
isHovering: true,
}));
document.body.classList.add("cursor-hover");
}
};
// 鼠标离开元素处理
const handleMouseLeave = () => {
setCursorState((prev) => ({
...prev,
cursorType: "default",
isHovering: false,
}));
document.body.classList.remove(
"cursor-link",
"cursor-button",
"cursor-text",
"cursor-disabled",
"cursor-loading",
"cursor-hover",
);
};
// 平滑动画更新
const updateCursor = () => {
const speed = 0.15;
const followerSpeed = 0.08;
// 主光标跟随
currentX += (mouseX - currentX) * speed;
currentY += (mouseY - currentY) * speed;
// 跟随光标
followerX += (mouseX - followerX) * followerSpeed;
followerY += (mouseY - followerY) * followerSpeed;
// 更新位置
if (cursorRef.current) {
cursorRef.current.style.transform =
`translate(${currentX}px, ${currentY}px)`;
}
if (followerRef.current) {
followerRef.current.style.transform =
`translate(${followerX}px, ${followerY}px)`;
}
animationFrameId = requestAnimationFrame(updateCursor);
};
// 磁性吸附效果
const handleMagneticElements = () => {
const magneticElements = document.querySelectorAll(".magnetic-element");
magneticElements.forEach((element) => {
const rect = element.getBoundingClientRect();
const centerX = rect.left + rect.width / 2;
const centerY = rect.top + rect.height / 2;
const distance = Math.sqrt(
Math.pow(mouseX - centerX, 2) + Math.pow(mouseY - centerY, 2),
);
if (distance < 100) {
const strength = (100 - distance) / 100;
const deltaX = (mouseX - centerX) * strength * 0.3;
const deltaY = (mouseY - centerY) * strength * 0.3;
(element as HTMLElement).style.transform =
`translate(${deltaX}px, ${deltaY}px)`;
} else {
(element as HTMLElement).style.transform = "translate(0px, 0px)";
}
});
};
// 事件监听器
document.addEventListener("mousemove", handleMouseMove);
document.addEventListener("mousedown", handleMouseDown);
document.addEventListener("mouseup", handleMouseUp);
// 使用事件委托处理悬停
document.addEventListener("mouseover", handleMouseEnter);
document.addEventListener("mouseout", handleMouseLeave);
// 启动动画循环
updateCursor();
// 磁性效果更新
const magneticInterval = setInterval(handleMagneticElements, 16);
// 清理函数
return () => {
document.removeEventListener("mousemove", handleMouseMove);
document.removeEventListener("mousedown", handleMouseDown);
document.removeEventListener("mouseup", handleMouseUp);
document.removeEventListener("mouseover", handleMouseEnter);
document.removeEventListener("mouseout", handleMouseLeave);
if (animationFrameId) {
cancelAnimationFrame(animationFrameId);
}
clearInterval(magneticInterval);
// 清理样式类
document.body.classList.remove(
"cursor-click",
"cursor-link",
"cursor-button",
"cursor-text",
"cursor-disabled",
"cursor-loading",
"cursor-hover",
);
};
}, []);
// 检测是否为移动设备
const [isMobile, setIsMobile] = useState(false);
useEffect(() => {
const checkMobile = () => {
setIsMobile(globalThis.innerWidth <= 768 || "ontouchstart" in window);
};
checkMobile();
globalThis.addEventListener("resize", checkMobile);
return () => globalThis.removeEventListener("resize", checkMobile);
}, []);
// 移动设备不显示自定义光标
if (isMobile) {
return null;
}
return (
<>
{/* 主光标 */}
<div
ref={cursorRef}
className="cursor fixed top-0 left-0 pointer-events-none z-[9999] mix-blend-mode-difference"
style={{
width: "20px",
height: "20px",
background: "rgba(59, 130, 246, 0.8)",
borderRadius: "50%",
transition: "all 0.1s ease",
transform: "translate(-50%, -50%)",
}}
/>
{/* 跟随光标 */}
<div
ref={followerRef}
className="cursor-follower fixed top-0 left-0 pointer-events-none z-[9998]"
style={{
width: "8px",
height: "8px",
background: "rgba(59, 130, 246, 1)",
borderRadius: "50%",
transition: "all 0.15s ease",
transform: "translate(-50%, -50%)",
}}
/>
{/* 光标指示器 */}
<div
className="cursor-indicator fixed pointer-events-none z-[9996] text-xs opacity-0 transition-opacity duration-200"
style={{
color: "rgba(59, 130, 246, 0.8)",
background: "rgba(255, 255, 255, 0.9)",
padding: "4px 8px",
borderRadius: "4px",
transform: "translate(-50%, -100%)",
}}
/>
</>
);
}
|