CSS animation 使用 @keyframes 定义关键帧,比 transition 更灵活,可以实现多步骤、循环的复杂动画。
CSS animation 包含多个子属性:
.animation {
animation-name: myAnimation;
animation-duration: 1s;
animation-timing-function: ease;
animation-delay: 0s;
animation-iteration-count: 1;
animation-direction: normal;
animation-fill-mode: forwards;
animation-play-state: running;
}
.animation {
animation: myAnimation 1s ease 0s 1 normal forwards running;
}
在 Vue 中使用 CSS 动画:
<div id="app">
<button @click="show = !show">切换</button>
<transition name="bounce">
<div v-if="show" class="box">Hello</div>
</transition>
</div>
<style>
.box {
width: 100px;
height: 100px;
background: #42b983;
display: flex;
align-items: center;
justify-content: center;
color: white;
}
.bounce-enter-active {
animation: bounce-in 0.5s;
}
.bounce-leave-active {
animation: bounce-in 0.5s reverse;
}
@keyframes bounce-in {
0% {
transform: scale(0);
}
50% {
transform: scale(1.2);
}
100% {
transform: scale(1);
}
}
</style>
<script>
new Vue({
el: '#app',
data: { show: true }
})
</script>
注意:使用 animation 时,不需要定义 v-enter 和 v-leave-to 类。
@keyframes bounce {
0%, 100% {
transform: translateY(0);
}
25% {
transform: translateY(-30px);
}
50% {
transform: translateY(-15px);
}
75% {
transform: translateY(-8px);
}
}
.bounce-enter-active {
animation: bounce 0.6s ease;
}
@keyframes shake {
0%, 100% {
transform: translateX(0);
}
10%, 30%, 50%, 70%, 90% {
transform: translateX(-10px);
}
20%, 40%, 60%, 80% {
transform: translateX(10px);
}
}
.shake-enter-active {
animation: shake 0.5s ease;
}
@keyframes pulse {
0% {
transform: scale(1);
}
50% {
transform: scale(1.1);
}
100% {
transform: scale(1);
}
}
.pulse-enter-active {
animation: pulse 0.5s ease;
}
@keyframes flip-in {
0% {
transform: perspective(400px) rotateY(90deg);
opacity: 0;
}
40% {
transform: perspective(400px) rotateY(-20deg);
}
60% {
transform: perspective(400px) rotateY(10deg);
}
80% {
transform: perspective(400px) rotateY(-5deg);
}
100% {
transform: perspective(400px) rotateY(0);
opacity: 1;
}
}
.flip-enter-active {
animation: flip-in 0.8s ease;
}
@keyframes gradient {
0% {
background-position: 0% 50%;
}
50% {
background-position: 100% 50%;
}
100% {
background-position: 0% 50%;
}
}
.gradient-box {
background: linear-gradient(270deg, #42b983, #35495e, #42b983);
background-size: 200% 200%;
animation: gradient 3s ease infinite;
}
@keyframes typing {
from {
width: 0;
}
to {
width: 100%;
}
}
@keyframes blink-caret {
from, to {
border-color: transparent;
}
50% {
border-color: #42b983;
}
}
.typing {
overflow: hidden;
border-right: 2px solid #42b983;
white-space: nowrap;
animation: typing 3.5s steps(40, end), blink-caret 0.75s step-end infinite;
}
@keyframes fade-slide-up {
0% {
opacity: 0;
transform: translateY(30px);
}
100% {
opacity: 1;
transform: translateY(0);
}
}
.fade-slide-up-enter-active {
animation: fade-slide-up 0.5s ease forwards;
}
.fade-slide-up-leave-active {
animation: fade-slide-up 0.5s ease reverse;
}
@keyframes rotate-in {
0% {
transform: rotate(-180deg) scale(0);
opacity: 0;
}
100% {
transform: rotate(0) scale(1);
opacity: 1;
}
}
.rotate-in-enter-active {
animation: rotate-in 0.6s cubic-bezier(0.68, -0.55, 0.265, 1.55);
}
多个属性同时变化:
@keyframes complex-animation {
0% {
opacity: 0;
transform: translateY(50px) rotate(-10deg) scale(0.8);
}
50% {
transform: translateY(-10px) rotate(5deg) scale(1.05);
}
100% {
opacity: 1;
transform: translateY(0) rotate(0) scale(1);
}
}
.complex-enter-active {
animation: complex-animation 0.6s ease;
}
控制动画开始前和结束后的状态:
.forwards {
animation-fill-mode: forwards;
}
.backwards {
animation-fill-mode: backwards;
}
.both {
animation-fill-mode: both;
}
在 Vue 中,通常使用默认值即可,因为 Vue 会自动处理元素状态。
<transition name="notification">
<div v-if="show" class="notification">
操作成功!
</div>
</transition>
<style>
.notification {
position: fixed;
top: 20px;
right: 20px;
padding: 15px 20px;
background: #42b983;
color: white;
border-radius: 4px;
}
.notification-enter-active {
animation: slide-in-right 0.3s ease;
}
.notification-leave-active {
animation: slide-out-right 0.3s ease;
}
@keyframes slide-in-right {
from {
transform: translateX(100%);
opacity: 0;
}
to {
transform: translateX(0);
opacity: 1;
}
}
@keyframes slide-out-right {
from {
transform: translateX(0);
opacity: 1;
}
to {
transform: translateX(100%);
opacity: 0;
}
}
</style>
@keyframes spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
.spinner {
width: 40px;
height: 40px;
border: 3px solid #f3f3f3;
border-top: 3px solid #42b983;
border-radius: 50%;
animation: spin 1s linear infinite;
}
选择建议:
| 特性 | transition | animation |
|---|---|---|
| 复杂度 | 简单 | 复杂 |
| 关键帧 | 只有开始和结束 | 可定义多个 |
| 循环 | 不支持 | 支持 |
| 触发 | 需要状态变化 | 可自动播放 |
| 适用场景 | 简单过渡 | 复杂动画 |
经验法则:
CSS 动画让过渡效果更加丰富。接下来学习如何集成第三方动画库,快速实现专业效果。