黄子涵

CSS 动画

CSS 动画用法同 CSS 过渡,区别是在动画中 v-enter 类名在节点插入 DOM 后不会立即删除,而是在 animationend 事件触发时删除。

示例:(省略了兼容性前缀)

<div id="example-2">
  <button @click="show = !show">Toggle show</button>
  <transition name="bounce">
    <p v-if="show">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris facilisis enim libero, at lacinia diam fermentum id. Pellentesque habitant morbi tristique senectus et netus.</p>
  </transition>
</div>
new Vue({
  el: '#example-2',
  data: {
    show: true
  }
})
.bounce-enter-active {
  animation: bounce-in .5s;
}
.bounce-leave-active {
  animation: bounce-in .5s reverse;
}
@keyframes bounce-in {
  0% {
    transform: scale(0);
  }
  50% {
    transform: scale(1.5);
  }
  100% {
    transform: scale(1);
  }
}
<!DOCTYPE html>
<html lang="zh">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>CSS 动画</title>
  <script src="./vue.js"></script>
  <style>
    .hzhBounce-enter-active {
      animation: hzh-bounce-in .5s;
    }

    .hzhBounce-leave-active {
      animation: hzh-bounce-in .5s reverse;
    }

    @keyframes hzh-bounce-in {
      0% {
        transform: scale(0);
      }
      50% {
        transform: scale(1.5);
      }
      100% {
        transform: scale(1);
      }
    }
  </style>
</head>

<body>
  <div id="hzh">
    <button @click="hzhShow = !hzhShow">
      切换显示
    </button>
    <transition name="hzhBounce">
      <p v-if="hzhShow">
        黄子涵是帅哥!<br>
        黄子涵是靓仔!<br>
        黄子涵真厉害!<br>
        黄子涵真聪明!
      </p>
    </transition>
  </div>
  <script>
    new Vue({
      el: '#hzh',
      data: {
        hzhShow: true
      }
    })
  </script>
</body>

image

posted @ 2022-06-05 20:34  黄子涵  阅读(21)  评论(0编辑  收藏  举报