当前位置:   article > 正文

详解 Vue 过渡 transition 动画 animation 并结合第三方库 animation.css 和 gsap_transition gsap

transition gsap

transition   vue过渡组件

标签自带类名

触发时机默认类名

自定义类名

<transition name="xxx">

自定义行内式类名 方便结合第三方库

transition 钩子 接收参数el

enter 和leave 第二个参数 done 可以 决定 

after-enter  after-leave

的 周期内的执行时机

v-if进入进入进入进入
v-show

v-enter-from

v-enter-active

v-enter-to

xxx-enter-from

xxx-enter-active

xxx-enter-to

enter-from-class

enter-active-class

enter-to-class 

@before-enter="beforeEnter"
@enter="enter"
@after-enter="afterEnter"
@enter-cancelled="enterCancelled"

<component :is="xxx" >离开

离开

离开离开
标签元素

v-leave-from

v-leave-active

v-leave-to

xxx-leave-from

xxx-leave-active

xxx-leave-to

leave-class

leave-active-class

leave-to-class

@before-leave="beforeLeave"
@leave="leave"
@after-leave="afterLeave"
@leave-cancelled="leaveCancelled"

在 xxx-enter-active    xxx-leave-active  时可以做 一些过度属性

  1. .slide-leave-from {
  2. width: 200px;
  3. height: 200px;
  4. }
  5. .slide-leave-active {
  6. transition: all 1s;
  7. animation: bounce-in 0.5s reverse;
  8. }
  9. .slide-leave-to {
  10. width: 0;
  11. height: 0;
  12. }

 使用自定义类名后  enter-from-class="from"  enter-to-class = "to"

  1. .from {
  2. width: 0;
  3. height: 0;
  4. }
  5. .slide-enter-active {
  6. transition: all 1s;
  7. animation: bounce-in 0.5s;
  8. }
  9. .to {
  10. width: 200px;
  11. height: 200px;
  12. }

代码

  1. <template>
  2. <div>
  3. <el-button @click="boo = !boo">toggle</el-button>
  4. <Transition
  5. v-for="(item,index) in data" :key="index"
  6. :enter-active-class="item.Inclass"
  7. :leave-active-class="item.Outclass">
  8. <h1 v-if="boo" >{{ item.des }}</h1>
  9. </Transition>
  10. </div>
  11. </template>
  12. <script lang="ts" setup>
  13. import { ref,onMounted,reactive } from "vue";
  14. import "animate.css";
  15. type data = {
  16. des:string,
  17. Inclass:string,
  18. Outclass:string
  19. }
  20. let data = reactive<data[]>([
  21. {
  22. des:'我是第一条数据',
  23. Inclass:'animate__animated animate__fadeInLeftBig',
  24. Outclass:'animate__animated animate__fadeOutLeftBig'
  25. },
  26. {
  27. des:'我是第二条数据',
  28. Inclass:'animate__animated animate__fadeInLeft',
  29. Outclass:'animate__animated animate__fadeOutLeft'
  30. },
  31. {
  32. des:'我是第三条数据',
  33. Inclass:'animate__animated animate__fadeInUpBig',
  34. Outclass:'animate__animated animate__fadeOutUpBig'
  35. },
  36. {
  37. des:'我是第四条数据',
  38. Inclass:'animate__animated animate__flipInX',
  39. Outclass:'animate__animated animate__flipOutX'
  40. },
  41. {
  42. des:'我是第五条数据',
  43. Inclass:'animate__animated animate__zoomInDown',
  44. Outclass:'animate__animated animate__zoomOutDown'
  45. },
  46. {
  47. des:'我是第六条数据',
  48. Inclass:'animate__animated animate__backInLeft',
  49. Outclass:'animate__animated animate__backOutLeft'
  50. },
  51. ])
  52. let boo = ref<boolean>(false);
  53. </script>
  54. <style scoped lang="scss">
  55. .box {
  56. width: 200px;
  57. height: 200px;
  58. background-color: #ccc;
  59. }
  60. .from {
  61. width: 0;
  62. height: 0;
  63. }
  64. .slide-enter-active {
  65. transition: all 1s;
  66. animation: bounce-in 0.5s;
  67. }
  68. .to {
  69. width: 200px;
  70. height: 200px;
  71. }
  72. /**
  73. 离开
  74. */
  75. .slide-leave-from {
  76. width: 200px;
  77. height: 200px;
  78. }
  79. .slide-leave-active {
  80. transition: all 1s;
  81. animation: bounce-in 0.5s reverse;
  82. }
  83. .slide-leave-to {
  84. width: 0;
  85. height: 0;
  86. }
  87. @keyframes bounce-in {
  88. 0% {
  89. transform: scale(0);
  90. }
  91. 50% {
  92. transform: scale(1.5);
  93. }
  94. 100% {
  95. transform: scale(1);
  96. }
  97. }
  98. .font{
  99. margin: auto;
  100. width: 300px;
  101. }
  102. </style>

使用动画库 animate.css

animate.css 官网

  1. npm install animate.css --save
  2. import "animate.css"; //组件中引入

使用时 加固定前缀 animate__animated  后面的类名按官网需求添加

<h1 class="animate__animated animate__bounce">An animated element</h1>

效果

 

学习 gsap 动画库 ☆      有助于学习 three.js 

greensock 绿袜官网

 

 结合  transition 生命周期 和 gsap

  1. let beforeEnter = (el) => {
  2. gsap.set(el,{
  3. background:'#ccc'
  4. })
  5. };
  6. let enter = (el,done) => {
  7. gsap.set(el,{
  8. backgroundColor:'pink'
  9. })
  10. };
  11. let afterEnter = () => {
  12. console.log(555);
  13. };
  14. let leave = (el,done)=>{
  15. gsap.set(el,{
  16. backgroundColor:'#ccc'
  17. })
  18. }
  19. <Transition
  20. v-on:before-enter="beforeEnter"
  21. v-on:enter="enter"
  22. v-on:after-enter="afterEnter"
  23. v-for="(item, index) in data"
  24. :key="index"
  25. :enter-active-class="item.Inclass"
  26. :leave-active-class="item.Outclass"
  27. @leave="leave"
  28. >

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/421273
推荐阅读
相关标签
  

闽ICP备14008679号