官方文档

过渡transition:从一个状态到另一个状态
动画animation:元素的运动

CSS实现过渡和动画

在vue中,可以通过基本的CSS实现过渡和动画

  1. <style>
  2. .color-transition {
  3. transition: background-color 1s ease;
  4. }
  5. </style>
  6. <script>
  7. const app = Vue.createApp({
  8. data(){
  9. return {
  10. styleObj: {
  11. background: 'green'
  12. }
  13. }
  14. },
  15. methods:{
  16. handleClick(){
  17. if(this.styleObj.background === 'green'){
  18. this.styleObj.background = 'blue'
  19. }else{
  20. this.styleObj.background = 'green'
  21. }
  22. }
  23. },
  24. template: `
  25. <div class="color-transition" :style="styleObj" @click="handleClick">hello world</div>
  26. `
  27. })
  28. </script>

进入/离开过渡

官方文档
使用transiton封装组件

  1. <div id="demo">
  2. <button @click="show = !show">
  3. Toggle
  4. </button>
  5. <transition name="fade">
  6. <p v-if="show">hello</p>
  7. </transition>
  8. </div>
  1. const Demo = {
  2. data() {
  3. return {
  4. show: true
  5. }
  6. }
  7. }
  8. Vue.createApp(Demo).mount('#demo')
  1. .fade-enter-active,
  2. .fade-leave-active {
  3. transition: opacity 0.5s ease;
  4. }
  5. .fade-enter-from,
  6. .fade-leave-to {
  7. opacity: 0;
  8. }

过渡类名可以自定义,参考官方文档

组件/元素切换动画

文档

  1. <style>
  2. .v-enter-from,.v-leave-to {
  3. opacity: 0;
  4. }
  5. .v-enter-to,.v-leave-from {
  6. opacity: 1;
  7. }
  8. .v-enter-active,.v-leave-active {
  9. transition: all 1s ease-in;
  10. }
  11. </style>
  12. <script>
  13. const app = Vue.createApp({
  14. data(){
  15. return {
  16. show: true
  17. }
  18. },
  19. methods:{
  20. handleClick(){
  21. this.show = !this.show
  22. }
  23. },
  24. template: `
  25. <transition mode="out-in" appear>
  26. <div v-if="show">hello</div>
  27. <div v-else>world</div>
  28. </transition>
  29. <button @click="handleClick">切换</button>
  30. `
  31. })
  32. app.mount("#root")
  33. </script>

vue的切换默认是同时发生的,mode设置为out-in或in-out控制切换顺序,appear让元素第一次出现时有动画
组件的切换可以用composition动态组件实现

列表过渡

文档

  1. <style>
  2. span {
  3. display: inline-block;
  4. padding: 0 10px;
  5. }
  6. .v-enter-from {
  7. opacity: 0;
  8. transform: translateY(20px);
  9. }
  10. .v-enter-to {
  11. opacity: 1;
  12. }
  13. .v-enter-active {
  14. transition: all 1s ease-in;
  15. }
  16. .v-move {
  17. transition: all 1s ease-in;
  18. }
  19. </style>
  20. <script>
  21. const app = Vue.createApp({
  22. data(){
  23. return {
  24. list: [1,2,3]
  25. }
  26. },
  27. methods:{
  28. handleClick(){
  29. this.list.unshift(this.list.length + 1)
  30. }
  31. },
  32. template: `
  33. <transition-group>
  34. <span v-for="item in list" :key="item"> {{item}} </span>
  35. </transition-group>
  36. <button @click="handleClick">增加</button>
  37. `
  38. })
  39. app.mount("#root")
  40. </script>
  • 使用transiton-group标签给列表添加过渡
  • .v-mode给挪动位置的元素添加过渡样式

    状态过渡

    文档
    1. <script>
    2. const app = Vue.createApp({
    3. data(){
    4. return {
    5. number: 1,
    6. animateNumber: 1
    7. }
    8. },
    9. methods:{
    10. handleClick(){
    11. this.number = 10
    12. if (this.animateNumber < this.number) {
    13. const animation = setInterval(()=>{
    14. this.animateNumber += 1;
    15. if (this.animateNumber === 10){
    16. clearInterval(animation)
    17. }
    18. },100)
    19. }
    20. }
    21. },
    22. template: `
    23. <div>{{animateNumber}}</div>
    24. <button @click="handleClick">增加</button>
    25. `
    26. })
    27. app.mount("#root")
    28. </script>
    当数字从1增加到10时,显示中间变化的过程