/
features.js
184 lines (160 loc) · 5.19 KB
/
features.js
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import gsap from "gsap";
import { ScrollTrigger } from "gsap/dist/ScrollTrigger";
import { ScrollToPlugin } from "gsap/dist/ScrollToPlugin";
gsap.registerPlugin(ScrollTrigger, ScrollToPlugin);
export default function initFeatures(element) {
if (!element) return;
const pin = element.querySelectorAll(".features-pin");
const progress = element.querySelector(".progress");
const progressRect = progress.getBoundingClientRect();
const panels = element.querySelectorAll(".panel");
const media = element.querySelectorAll(".media-wrap img:not(:first-child), .media-wrap video:not(:first-child)");
const lazyVideos = element.querySelectorAll("video[data-src]");
const lists = element.querySelectorAll(".benefit ul");
const contents = element.querySelectorAll(".heading, .benefit");
const contentRects = Array.from(contents).map((el) => {
return el.getBoundingClientRect();
});
const scrollTriggers = element.querySelectorAll("[data-scroll-target]");
ScrollTrigger.create({
trigger: element,
start: "top top",
end: "bottom bottom",
onEnter: () => {
lazyVideos.forEach((el) => {
if (!el.src && el.offsetWidth > 0 && el.offsetHeight > 0) {
el.src = el.dataset.src;
el.load();
}
});
},
});
let matchMedia = gsap.matchMedia();
matchMedia.add("(min-width: 1200px) and (min-height: 768px)", () => {
gsap.set(
media,
{
yPercent: 100,
scale: 1.5,
},
0
);
gsap.set(
lists,
{
height: 0,
},
0
);
const contentHeadings = element.querySelectorAll(".features-content h1, .features-content h2");
const contentHeadingRects = Array.from(contentHeadings).map((el) => {
return el.getBoundingClientRect();
});
const timeline = gsap.timeline({
paused: true,
});
timeline.set(
progress,
{
scaleY: contentRects[0].height / 100,
},
0
);
panels.forEach((el, i) => {
const label = el.id;
timeline.addLabel(label, i);
// Skip the last panel
if (i === panels.length - 1) {
return;
}
timeline.to(
media[i],
{
yPercent: 0,
scale: 1,
ease: "none",
duration: 1,
},
i
);
if (i > 0) {
timeline.to(
lists[i - 1],
{
height: 0,
ease: "none",
duration: 0.5,
},
i
);
}
timeline.to(
lists[i],
{
height: "auto",
ease: "none",
duration: 0.5,
},
i
);
timeline.to(
progress,
{
y: () => {
return contentHeadingRects[i + 1].y - progressRect.y + 10;
},
scaleY: () => {
return contentRects[i + 1].height / 100;
},
ease: "power2.out",
duration: 1,
},
i
);
});
const snap = gsap.utils.snap(1 / (panels.length - 1));
const scrollTimeline = gsap.timeline({
scrollTrigger: {
trigger: element,
start: "top top",
end: "bottom bottom",
pin: pin,
scrub: true,
onUpdate: (self) => {
const snappedProgress = snap(self.progress);
gsap.to(timeline, {
progress: snappedProgress,
duration: 1,
ease: "power2.out",
});
},
},
});
scrollTimeline.to(
{ progress: 0 },
{
progress: 1,
duration: panels.length - 1,
}
);
panels.forEach((el, i) => {
const label = el.id;
scrollTimeline.addLabel(label, i);
});
scrollTimeline.duration(panels.length);
// Add click handlers to scroll timeline from [data-scroll-target] to associated timeline label
scrollTriggers.forEach((el) => {
el.addEventListener("click", () => {
const target = el.dataset.scrollTarget;
gsap.to(window, {
scrollTo: {
y: scrollTimeline.scrollTrigger.labelToScroll(target),
duration: 1,
ease: "power2.out",
autoKill: true,
},
});
});
});
});
}