利用GSAP简单实现首页的过渡动画

https://mock.ezcomezgo.com/gsap-demo-1/

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
<template>
<div class="header">
<div class="title">Title</div>
<div class="nav">
<div class="nav-item">nav-item-1</div>
<div class="nav-item">nav-item-2</div>
<div class="nav-item">nav-item-3</div>
<div class="nav-item">nav-item-4</div>
<div class="nav-item">nav-item-5</div>
</div>
<div class="menu">Menu</div>
</div>
<div class="body">
<div class="content">
<div class="title">Title</div>
<div class="desc">Lorem ipsum dolor sit amet consectetur adipisicing elit. Vero eos repellendus aliquam facilis
provident. Similique sapiente, harum obcaecati molestias inventore accusantium eum aliquid nihil suscipit
molestiae laboriosam, aliquam modi ipsum.</div>
</div>
<div class="bgc"></div>
</div>
</template>

<style>
:root {
--bgc: #1c073e;
--header-high: 80px;
--body-heigh: calc(100vh - var(--header-high));
--menu-width: 400px;
--padding-left: 40px;
}

html,
body {
all: unset;
display: flex;
flex-direction: column;
justify-content: flex-start;
align-content: center;
color: #fff;
background-color: var(--bgc);
}

*,
*::before,
*::after {
box-sizing: border-box;
padding: 0;
margin: 0;
text-decoration: none;
list-style: none;
-webkit-user-drag: none;
user-select: none;
}

.header,
.body {
width: 100vw;
padding: 0px var(--padding-left);
overflow: hidden;
}

.header {
height: var(--header-high);
display: flex;
flex-direction: row;
justify-content: space-between;
align-items: center;
}

.body {
height: var(--body-heigh);
}

.header .title {
font-size: 24px;
}

.header .title,
.header .nav {
display: flex;
justify-content: center;
align-items: center;
transition: .3s;
}

.header .nav {
flex-direction: row;
height: calc(var(--header-high) / 2);
}

.header .nav .nav-item {
height: 100%;
display: flex;
justify-content: center;
align-items: center;
min-width: 100px;
position: relative;
display: flex;
justify-content: center;
align-items: center;
flex-direction: column;
}

.header .nav .nav-item::before {
transition: .3s;
position: absolute;
content: '';
height: 5px;
background-color: rgb(255, 255, 255);
bottom: 0px;
width: 0px;
border-radius: 3px;
}

.header .nav .nav-item:hover::before {
width: 80%;
}

.header .menu {
display: none;
transition: .3s;
}

.body {
display: flex;
flex-direction: row;
justify-content: flex-start;
align-items: center;
position: relative;
}

.body .content {
position: relative;
z-index: 2;
opacity: 1;
width: 700px;
display: flex;
flex-direction: column;
justify-content: space-around;
align-items: flex-start;
flex-wrap: wrap;
}

.body .content .title,
.body .content .desc {
display: flex;
justify-content: center;
align-items: center;
font-size: 22px;
}

.body .content .title {
font-size: 30px;
font-weight: bold;
}

.body .bgc {
width: 100%;
height: 100%;
background-image: url('./assets/elefant.svg');
background-size: 600px 600px;
background-position: 300px;
background-repeat: no-repeat;
}

@media screen and (max-width: 900px) {
.header .menu {
display: block;
}

.header .nav {
width: 100%;
display: none;
}
}
</style>
<script setup>
import gsap from 'gsap'
import { onMounted } from 'vue';
onMounted(() => {
const tween = gsap.timeline({ delay: 0.5 })
tween.from('.header', {
duration: 0.5,
ease: 'power2',
y: -500,
})
tween.from('.body .content', {
duration: 0.8,
ease: 'power2',
y: 200,
opacity: 0
})
tween.from('.body .bgc', {
duration: 1.2,
ease: 'power2',
x: 400,
opacity: 0
})
})
</script>