-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathscript.js
More file actions
72 lines (64 loc) · 2.12 KB
/
script.js
File metadata and controls
72 lines (64 loc) · 2.12 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
document.addEventListener("DOMContentLoaded", () => {
gsap.registerPlugin(ScrollTrigger);
// smooth scroll
const lenis = new Lenis();
lenis.on("scroll", ScrollTrigger.update);
gsap.ticker.add((time) => {
lenis.raf(time * 1000);
});
gsap.ticker.lagSmoothing(0);
const cards = gsap.utils.toArray(".card");
const totalScrollHeight = window.innerHeight * 3;
const positions = [14, 38, 62, 86];
const rotations = [-15, -7.5, 7.5, 15];
// pin the cards section
ScrollTrigger.create({
trigger: ".cards",
start: "top top",
end: () => `+=${totalScrollHeight}`,
pin: true,
pinSpacing: true,
});
// spread cards
cards.forEach((card, index) => {
gsap.to(card, {
left: `${positions[index]}%`,
rotation: `${rotations[index]}`,
ease: "none",
scrollTrigger: {
trigger: ".cards",
start: "top top",
end: () => `+=${window.innerHeight}`,
scrub: 0.5,
id: `spread-${index}`,
},
});
});
// rotate and flip cards with staggered effect
cards.forEach((card, index) => {
const frontEl = card.querySelector(".flip-card-front");
const backEl = card.querySelector(".flip-card-back");
const staggerOffset = index * 0.05;
const startOffset = 1 / 3 + staggerOffset;
const endOffset = 2 / 3 + staggerOffset;
ScrollTrigger.create({
trigger: ".cards",
start: "top top",
end: () => `+=${totalScrollHeight}`,
scrub: 1,
id: `rotate-flip-${index}`,
onUpdate: (self) => {
const progress = self.progress;
if (progress >= startOffset && progress <= endOffset) {
const animationProgress = (progress - startOffset) / (1 / 3);
const frontRotation = -180 * animationProgress;
const backRotation = 180 - 180 * animationProgress;
const cardRotation = rotations[index] * (1 - animationProgress);
frontEl.style.transform = `rotateY(${frontRotation}deg)`;
backEl.style.transform = `rotateY(${backRotation}deg)`;
card.style.transform = `translate(-50%, -50%) rotate(${cardRotation}deg)`;
}
},
});
});
});