CSS 动画


alias: vue-css-animations keywords: Vue2, CSS动画, animation, keyframes, 帧动画 description: Vue.js CSS动画实现,使用@keyframes创建复杂的动画效果。

CSS 动画

CSS animation 使用 @keyframes 定义关键帧,比 transition 更灵活,可以实现多步骤、循环的复杂动画。

animation 属性

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-enterv-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;
}

animation-fill-mode

控制动画开始前和结束后的状态:

.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 vs animation

选择建议:

特性transitionanimation
复杂度简单复杂
关键帧只有开始和结束可定义多个
循环不支持支持
触发需要状态变化可自动播放
适用场景简单过渡复杂动画

经验法则:

  • 简单的 A 到 B 过渡 → 使用 transition
  • 需要多步骤或循环 → 使用 animation

CSS 动画让过渡效果更加丰富。接下来学习如何集成第三方动画库,快速实现专业效果。