You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
// lenis is the object returned from useLenis, and it has a method called scrollTo() that takes two arguments, the first is the id of the element you want to scroll to, and the second is the options object, here we set the lerp to 0.01 to make the scroll slower
Copy file name to clipboardExpand all lines: src/components/Parallax.jsx
+12-8Lines changed: 12 additions & 8 deletions
Original file line number
Diff line number
Diff line change
@@ -5,32 +5,36 @@ import { useWindowSize } from "@studio-freight/hamo";
5
5
import{ScrollTrigger}from"gsap/ScrollTrigger";
6
6
7
7
exportfunctionParallax({ className, children, speed =1, id ="parallax"}){
8
-
consttrigger=useRef();
9
-
consttarget=useRef();
10
-
consttimeline=useRef();
8
+
consttrigger=useRef();// this is the element that will trigger the animation
9
+
consttarget=useRef();// this is the element that will be animated
10
+
consttimeline=useRef();// this is the timeline of the animation that will be created by gsap
11
11
const{width: windowWidth}=useWindowSize();
12
12
13
13
useEffect(()=>{
14
14
gsap.registerPlugin(ScrollTrigger);
15
15
consty=windowWidth*speed*0.1;
16
+
// here the y is the distance the element will move in px when the trigger is at the top of the viewport and the element is at the bottom of the viewport
// here we create a function that will set the y position of the element, The gsap.quickSetter() method is a handy way to create a function that will set a specific property on a specific object. In this case, we want to set the y property of the target element in pixels.
18
21
19
22
timeline.current=gsap.timeline({
20
23
scrollTrigger: {
21
24
id: id,
22
-
trigger: trigger.current,
23
-
scrub: true,
24
-
start: "top bottom",
25
-
end: "bottom top",
25
+
trigger: trigger.current,// this is the element that will trigger the animation
26
+
scrub: true,// this will make the animation smooth and not jumpy when scrolling up and down the page
27
+
start: "top bottom",// this means the animation will start when the top of the trigger element reaches the bottom of the viewport
28
+
end: "bottom top",// this means the animation will end when the bottom of the trigger element reaches the top of the viewport
26
29
onUpdate: (e)=>{
27
30
setY(e.progress*y);
28
31
},
32
+
// markers: true,
29
33
},
30
34
});
31
35
32
36
return()=>{
33
-
timeline?.current?.kill();
37
+
timeline?.current?.kill();// this will kill the animation when the component unmounts
0 commit comments