-
Notifications
You must be signed in to change notification settings - Fork 0
/
gsap01.html
278 lines (260 loc) · 8.47 KB
/
gsap01.html
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>gsap01</title>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/gsap.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/ScrollTrigger.min.js"></script>
<link href="https://webfontworld.github.io/NexonLv1Gothic/NexonLv1Gothic.css" rel="stylesheet">
<link rel="stylesheet" href="assets/css/common.css">
</head>
<style>
#parallax__cont {
overflow: hidden;
}
.parallax__item {
width: 100%;
height: 100vh;
position: relative;
display: flex;
align-items: center;
justify-content: center;
}
.parallax__item:nth-child(2n) {
background-color: #222;
}
.parallax__item__num {
position: absolute;
right: 20px;
bottom: 20px;
font-size: 5vw;
line-height: 1;
}
.parallax__item__img {
width: 10vw;
height: 10vw;
background-color: #fff;
background-size: cover;
background-position: center;
}
.parallax__item:nth-child(1) .parallax__item__img {
background-image: url(assets/img/images14.jpg);
}
.parallax__item:nth-child(2) .parallax__item__img {
background-image: url(assets/img/images15.jpg);
}
.parallax__item:nth-child(3) .parallax__item__img {
background-image: url(assets/img/images03.jpg);
}
.parallax__item:nth-child(4) .parallax__item__img {
background-image: url(assets/img/images04.jpg);
}
.parallax__item:nth-child(5) .parallax__item__img {
background-image: url(assets/img/images05.jpg);
}
.parallax__item:nth-child(6) .parallax__item__img {
background-image: url(assets/img/images06.jpg);
}
.parallax__item:nth-child(7) .parallax__item__img {
background-image: url(assets/img/images07.jpg);
}
.parallax__item:nth-child(8) .parallax__item__img {
background-image: url(assets/img/images08.jpg);
}
.parallax__item:nth-child(9) .parallax__item__img {
background-image: url(assets/img/images09.jpg);
}
.parallax__item__img.active {
filter: hue-rotate(100deg);
}
</style>
<body>
<main id="parallax__cont">
<section id="section1" class="parallax__item">
<span class="parallax__item__num">01</span>
<div class="parallax__item__img"></div>
</section>
<section id="section2" class="parallax__item">
<span class="parallax__item__num">02</span>
<div class="parallax__item__img"></div>
</section>
<section id="section3" class="parallax__item">
<span class="parallax__item__num">03</span>
<div class="parallax__item__img"></div>
</section>
<section id="section4" class="parallax__item">
<span class="parallax__item__num">04</span>
<div class="parallax__item__img"></div>
</section>
<section id="section5" class="parallax__item">
<span class="parallax__item__num">05</span>
<div class="parallax__item__img"></div>
</section>
<section id="section6" class="parallax__item">
<span class="parallax__item__num">06</span>
<div class="parallax__item__img"></div>
</section>
<section id="section7" class="parallax__item">
<span class="parallax__item__num">07</span>
<div class="parallax__item__img"></div>
</section>
<section id="section8" class="parallax__item">
<span class="parallax__item__num">08</span>
<div class="parallax__item__img"></div>
</section>
<section id="section9" class="parallax__item">
<span class="parallax__item__num">09</span>
<div class="parallax__item__img"></div>
</section>
</main>
<script>
const box1 = document.querySelector("#section1 .parallax__item__img");
const box2 = document.querySelector("#section2 .parallax__item__img");
const box3 = document.querySelector("#section3 .parallax__item__img");
const box4 = document.querySelector("#section4 .parallax__item__img");
const box5 = document.querySelector("#section5 .parallax__item__img");
const box6 = document.querySelector("#section6 .parallax__item__img");
const box7 = document.querySelector("#section7 .parallax__item__img");
const box8 = document.querySelector("#section8 .parallax__item__img");
// 01
gsap.to(box1, {
x: '40vw',
duration: 2,
borderRadius: 100,
rotate: 360,
})
// 02: trigger 指定目标元素,当目标元素可视时开始动画
gsap.to(box2, {
x: '40vw',
duration: 2,
borderRadius: 100,
rotate: 360,
scrollTrigger: {
trigger: box2,
}
})
// 03: toggleActions (default: play none none none)
// 四个属性:
// onEnter: 当滚动元素进入触发元素时(从上自下)
// onLeave: 当滚动元素离开触发元素时(继续向下)
// onEnterBack: 当滚动元素回到触发元素(从下自上)
// onLeaveBack: 当滚动元素离开触发元素(继续向上)
// 可选属性:
// play: 播放动画
// pause: 暂停动画
// resume: 继续播放动画
// reverse: 反向播放动画
// restart: 重新开始动画
// reset: 将动画重置到初始状态,并暂停
// complete: 将动画快进至结束
// none: 将不执行任何操作
gsap.to(box3, {
x: '40vw',
duration: 2,
borderRadius: 100,
rotate: 360,
scrollTrigger: {
trigger: box3,
toggleActions: "play pause reverse reset",
}
})
// 04
// top: 目标元素距离某个位置时开始动画
// end: 目标元素距离某个位置时结束动画
// markers: 显示动画执行的标记点
gsap.to(box4, {
x: '40vw',
duration: 2,
borderRadius: 100,
rotate: 360,
scrollTrigger: {
trigger: box4,
toggleActions: "play none reverse none",
start: 'top 50%',
end: 'bottom 20%',
// markers: true
}
})
// 05 : scrub 将滚动条与动画绑定,滚动条滚动时播放动画
// Boolean: 开启/关闭
// Number: 动画延迟,使得过渡更平滑(数字越大越平滑,但是延迟越大)
gsap.to(box5, {
duration: 2,
x: 500,
rotation: 360,
borderRadius: 100,
scrollTrigger: {
trigger: box5,
start: "top 50%",
end: "bottom 20%",
scrub: 1, //true, 1, 2...
markers: false
}
});
// 06 : pin 将目标元素固定在窗口中,直到触发元素离开窗口时取消固定(相对位置不变)
// Boolean: 开启/关闭
// String | Element: 可以指定一个要固定的元素,通常是触发元素的一个子元素
gsap.to(box6, {
duration: 2,
x: 500,
rotation: 360,
borderRadius: 100,
scrollTrigger: {
trigger: box6,
start: "top 50%",
end: "bottom 200px",
scrub: true,
pin: true,
markers: false
}
});
// 07 : toggleClass 给目标元素添加或移除 className
// String: 添加或移除的 className
// Object: {targets: String, className: String,}
gsap.to(box7, {
duration: 2,
x: 500,
rotation: 360,
borderRadius: 100,
scrollTrigger: {
trigger: box7,
start: "top center",
// end: "bottom 20%",
scrub: true,
toggleClass: "active",
markers: false,
id: "box7"
}
});
// 08 : callback
// 常用方法:
// onEnter: 当触发元素进入视口时调用
// onLeave: 当触发元素离开视口时调用
// onEnterBack: 当触发元素从视口下方回滚进入视口时调用
// onLeaveBack: 当触发元素从视口下方回滚离开视口时调用
// onUpdate: 当触发元素在视口内滚动时持续调用
// onToggle: 当触发元素进入或离开视口时调用(onEnter 和 onLeave 的综合)
// onRefresh: 每次触发器刷新时调用
// onScrubComplete: 每次 scrub 完成时调用
gsap.to(box8, {
duration: 2,
x: 500,
rotation: 360,
borderRadius: 100,
scrollTrigger: {
trigger: box8,
start: "top center",
end: "bottom 20%",
scrub: true,
markers: true,
onEnter: (self) => {console.log(self)},
onLeave: () => {console.log("onLeave")},
onEnterBack: () => {console.log("onEnterBack")},
onLeaveBack: () => {console.log("onLeaveBack")},
onUpdate : (self) => {console.log("onUpdate", self.progress.toFixed(3))},
onToggle : (self) => {console.log("onToggle", self.isActive)},
}
});
</script>
</body>
</html>