Skip to content

Commit 6d44c66

Browse files
committed
refactor: remove mouse parallax effect from starry background
1 parent 1949476 commit 6d44c66

File tree

1 file changed

+4
-44
lines changed

1 file changed

+4
-44
lines changed

src/components/StarryBackground.jsx

Lines changed: 4 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
'use client';
22

3-
import React, { useEffect, useState, useRef } from 'react';
3+
import React, { useEffect, useState } from 'react';
44

55
const StarryBackground = ({ starCount = 240 }) => {
66
const [stars, setStars] = useState([]);
7-
const [mousePosition, setMousePosition] = useState({ x: 0, y: 0 });
87
const [isMobile, setIsMobile] = useState(false);
9-
const containerRef = useRef(null);
108

119
// 检测是否为移动端
1210
useEffect(() => {
@@ -41,44 +39,6 @@ const StarryBackground = ({ starCount = 240 }) => {
4139
generateStars();
4240
}, [starCount, isMobile]); // 添加isMobile作为依赖项
4341

44-
// 监听鼠标移动
45-
useEffect(() => {
46-
const handleMouseMove = (e) => {
47-
if (containerRef.current) {
48-
const rect = containerRef.current.getBoundingClientRect();
49-
const centerX = rect.width / 2;
50-
const centerY = rect.height / 2;
51-
52-
// 计算鼠标相对于容器中心的位置
53-
const x = (e.clientX - rect.left - centerX) / centerX;
54-
const y = (e.clientY - rect.top - centerY) / centerY;
55-
56-
setMousePosition({ x, y });
57-
}
58-
};
59-
60-
window.addEventListener('mousemove', handleMouseMove);
61-
return () => window.removeEventListener('mousemove', handleMouseMove);
62-
}, []);
63-
64-
65-
// 计算星星的位移
66-
const getStarTransform = (star) => {
67-
// 不同层级的鼠标位移系数(与鼠标移动方向相反)
68-
const mouseLayerMultipliers = {
69-
1: 1, // 最上层,位移最小
70-
2: 2, // 中间层
71-
3: 4, // 最下层,位移最大
72-
};
73-
74-
const mouseMultiplier = mouseLayerMultipliers[star.layer];
75-
76-
const offsetX = -mousePosition.x * mouseMultiplier;
77-
const offsetY = -mousePosition.y * mouseMultiplier;
78-
79-
return `translate(${offsetX}px, ${offsetY}px)`;
80-
};
81-
8242
// 根据层级获取星星的透明度和大小
8343
const getStarStyle = (star) => {
8444
const layerStyles = {
@@ -93,14 +53,14 @@ const StarryBackground = ({ starCount = 240 }) => {
9353
opacity: style.opacity,
9454
width: style.size,
9555
height: style.size,
96-
transform: getStarTransform(star),
97-
transition: 'transform 0.1s ease-out',
56+
// 保持静止:不使用 transform/transition
57+
transform: 'none',
58+
transition: 'none',
9859
};
9960
};
10061

10162
return (
10263
<div
103-
ref={containerRef}
10464
className="absolute top-0 left-0 w-full h-full z-0 overflow-hidden bg-[#191E1B]"
10565
>
10666
{stars.map((star) => (

0 commit comments

Comments
 (0)