1
+ // Get the tail effect element and the page container
2
+ const tailEffectElement = document . querySelector ( '.tail-effect' ) ;
3
+ const pageContainer = document . body ;
4
+
5
+ // Set the tail effect delay and easing function
6
+ const tailEffectDelay = 0.2 ;
7
+ const easingFunction = 'cubic-bezier(0.4, 0, 0.2, 1)' ;
8
+
9
+ // Add event listeners to track cursor movement
10
+ pageContainer . addEventListener ( 'mousemove' , ( event ) => {
11
+ const x = event . clientX ;
12
+ const y = event . clientY ;
13
+
14
+ // Update the tail effect element's position with a delay
15
+ requestAnimationFrame ( ( ) => {
16
+ tailEffectElement . style . transform = `translate(${ x } px, ${ y } px)` ;
17
+ tailEffectElement . style . transition = `transform ${ tailEffectDelay } s ${ easingFunction } ` ;
18
+ } ) ;
19
+ } ) ;
20
+
21
+ // Add event listeners to track cursor movement outside of navigation links
22
+ pageContainer . addEventListener ( 'mouseleave' , ( ) => {
23
+ tailEffectElement . style . transform = 'translate(0, 0)' ;
24
+ } ) ;
25
+
26
+ // Create a smooth trailing motion using a delay and easing function
27
+ let lastMouseX = 0 ;
28
+ let lastMouseY = 0 ;
29
+ let mouseX = 0 ;
30
+ let mouseY = 0 ;
31
+
32
+ function updateTailEffect ( ) {
33
+ mouseX += ( lastMouseX - mouseX ) * 0.1 ;
34
+ mouseY += ( lastMouseY - mouseY ) * 0.1 ;
35
+
36
+ tailEffectElement . style . transform = `translate(${ mouseX } px, ${ mouseY } px)` ;
37
+
38
+ requestAnimationFrame ( updateTailEffect ) ;
39
+ }
40
+
41
+ updateTailEffect ( ) ;
42
+
43
+ // Handle multiple tail effect elements (optional)
44
+ const tailEffectElements = document . querySelectorAll ( '.tail-effect' ) ;
45
+
46
+ tailEffectElements . forEach ( ( element ) => {
47
+ element . addEventListener ( 'mousemove' , ( event ) => {
48
+ const x = event . clientX ;
49
+ const y = event . clientY ;
50
+
51
+ requestAnimationFrame ( ( ) => {
52
+ element . style . transform = `translate(${ x } px, ${ y } px)` ;
53
+ element . style . transition = `transform ${ tailEffectDelay } s ${ easingFunction } ` ;
54
+ } ) ;
55
+ } ) ;
56
+ } ) ;
57
+
58
+ // Create a more complex animation path (optional)
59
+ function createAnimationPath ( ) {
60
+ const animationPath = [ ] ;
61
+ const numPoints = 10 ;
62
+
63
+ for ( let i = 0 ; i < numPoints ; i ++ ) {
64
+ const x = Math . random ( ) * window . innerWidth ;
65
+ const y = Math . random ( ) * window . innerHeight ;
66
+ animationPath . push ( { x, y } ) ;
67
+ }
68
+
69
+ return animationPath ;
70
+ }
71
+
72
+ const animationPath = createAnimationPath ( ) ;
73
+
74
+ let animationIndex = 0 ;
75
+
76
+ function updateAnimation ( ) {
77
+ const point = animationPath [ animationIndex ] ;
78
+ tailEffectElement . style . transform = `translate(${ point . x } px, ${ point . y } px)` ;
79
+
80
+ animationIndex = ( animationIndex + 1 ) % animationPath . length ;
81
+
82
+ requestAnimationFrame ( updateAnimation ) ;
83
+ }
84
+
85
+ // Update the last mouse position on mouse move
86
+ pageContainer . addEventListener ( 'mousemove' , ( event ) => {
87
+ lastMouseX = event . clientX ;
88
+ lastMouseY = event . clientY ;
89
+ } ) ;
90
+
91
+ // Start the animation
92
+ updateAnimation ( ) ;
0 commit comments