@@ -5,7 +5,6 @@ import React, { useEffect, useState, useRef } from 'react';
5
5
const StarryBackground = ( { starCount = 240 } ) => {
6
6
const [ stars , setStars ] = useState ( [ ] ) ;
7
7
const [ mousePosition , setMousePosition ] = useState ( { x : 0 , y : 0 } ) ;
8
- const [ scrollOffset , setScrollOffset ] = useState ( 0 ) ;
9
8
const [ isMobile , setIsMobile ] = useState ( false ) ;
10
9
const containerRef = useRef ( null ) ;
11
10
@@ -62,15 +61,6 @@ const StarryBackground = ({ starCount = 240 }) => {
62
61
return ( ) => window . removeEventListener ( 'mousemove' , handleMouseMove ) ;
63
62
} , [ ] ) ;
64
63
65
- // 监听页面滚动
66
- useEffect ( ( ) => {
67
- const handleScroll = ( ) => {
68
- setScrollOffset ( window . scrollY ) ;
69
- } ;
70
-
71
- window . addEventListener ( 'scroll' , handleScroll ) ;
72
- return ( ) => window . removeEventListener ( 'scroll' , handleScroll ) ;
73
- } , [ ] ) ;
74
64
75
65
// 计算星星的位移
76
66
const getStarTransform = ( star ) => {
@@ -81,18 +71,10 @@ const StarryBackground = ({ starCount = 240 }) => {
81
71
3 : 4 , // 最下层,位移最大
82
72
} ;
83
73
84
- // 不同层级的滚动偏移系数(轻微的视差效果)
85
- const scrollLayerMultipliers = {
86
- 1 : 0.001 , // 最上层,几乎不动
87
- 2 : 0.002 , // 中间层,轻微偏移
88
- 3 : 0.003 , // 最下层,稍多偏移
89
- } ;
90
-
91
74
const mouseMultiplier = mouseLayerMultipliers [ star . layer ] ;
92
- const scrollMultiplier = scrollLayerMultipliers [ star . layer ] ;
93
75
94
76
const offsetX = - mousePosition . x * mouseMultiplier ;
95
- const offsetY = - mousePosition . y * mouseMultiplier + scrollOffset * scrollMultiplier ;
77
+ const offsetY = - mousePosition . y * mouseMultiplier ;
96
78
97
79
return `translate(${ offsetX } px, ${ offsetY } px)` ;
98
80
} ;
0 commit comments