-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
58 lines (50 loc) · 1.45 KB
/
script.js
File metadata and controls
58 lines (50 loc) · 1.45 KB
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
// QUE: make the image scale down and move horizontally as the user scrolls using GSAP and ScrollTrigger, and Lenis for smooth scrolling
//IMP: prereq: gsap, ScrollTrigger, lenis
// Initialize Lenis
const lenis = new Lenis();
// Use requestAnimationFrame to continuously update the scroll
function raf(time) {
lenis.raf(time);
requestAnimationFrame(raf); // Call raf recursively
}
requestAnimationFrame(raf); // Start the raf loop
// Debounce raf to reduce the frequency of updates
let lastTime = 0;
const fpsInterval = 50; // Adjust this value to control the update frequency
function debouncedRaf(time) {
if (time - lastTime > fpsInterval) {
raf(time);
lastTime = time;
}
requestAnimationFrame(debouncedRaf);
}
requestAnimationFrame(debouncedRaf);
document.querySelectorAll(".elem").forEach(elem => {
let image = elem.querySelector("img");
let tl = gsap.timeline();
let xTransform = gsap.utils.random(-100, 100);
tl
.set(image, {
transformOrigin: `${xTransform < 0 ? 0 : '100%'}`,
}, "start")
.to(image, {
scale: 0,
ease: "none",
scrollTrigger: {
trigger: image,
start: "top top",
end: "bottom top",
scrub: true,
}
}, "start")
.to(elem, {
xPercent: xTransform,
ease: "none",
scrollTrigger: {
trigger: image,
start: "top bottom", // Corrected typo here
end: "bottom top",
scrub: true,
}
}, "start")
});