过渡 & 动画

  • v-enter:定义进入过渡的开始状态。在元素被插入之前生效,在元素被插入之后的下一帧移除。
  • v-enter-active:定义进入过渡生效时的状态。可以被用来定义进入过渡的过程时间,延迟和曲线函数。
  • v-enter-to: 定义进入过渡的结束状态。
  • v-leave: 定义离开过渡的开始状态。
  • v-leave-active:定义离开过渡生效时的状态。
  • v-leave-to:定义离开过渡的结束状态。
  1. <style>
  2. .section {
  3. width: 200px;
  4. height: 200px;
  5. background: #00ffff;
  6. }
  7. .v-enter {
  8. width: 0px;
  9. background: #00ff00;
  10. }
  11. .v-enter-active {
  12. background:#ff0000;
  13. transition: all 5s linear;
  14. }
  15. .v-enter-to {
  16. background: orange;
  17. }
  18. /* 进入状态结束 变成默认颜色 */
  19. .v-leave {
  20. background: #00ff00;
  21. }
  22. .v-leave-active {
  23. width: 0;
  24. background: rgb(238, 88, 200);
  25. transition: all 5s linear;
  26. }
  27. .v-leave-to {
  28. background: #29013b;
  29. }
  30. /* 离开状态结束 是消失 */
  31. </style>

JS 钩子动画

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>Document</title>
  8. <style>
  9. ul{
  10. list-style: none;
  11. margin: 0;
  12. padding: 0;
  13. }
  14. li {
  15. display: flex;
  16. align-items: center;
  17. margin: 20px;
  18. }
  19. li img {
  20. display: block;
  21. width: 150px;
  22. height: 80px;
  23. margin-right: 20px;
  24. }
  25. span {
  26. position: absolute;
  27. width: 150px;
  28. height: 80px;
  29. /* background: #00ffff; */
  30. transition: all 2s linear;
  31. }
  32. .cart {
  33. width: 150px;
  34. height: 50px;
  35. background: #00ff66;
  36. position: fixed;
  37. right: 50px;
  38. bottom: 50px;
  39. line-height: 50px;
  40. text-align: center;
  41. font-size: 25px;
  42. color: #ffffff;
  43. }
  44. </style>
  45. </head>
  46. <body>
  47. <div id="app">
  48. <ul>
  49. <!-- ref= "listArray" 专门用来获取 dom 元素 -->
  50. <li v-for = "(item,index) in lists" :key = `${index}_a` ref= "listArray" >
  51. <img :src="item.cover" alt="">
  52. <button @click = "addCart(index)">加入购物车</button>
  53. </li>
  54. </ul>
  55. <transition @enter = "enter" @after-enter = "after">
  56. <span v-if="isShow"></span>
  57. </transition>
  58. <div class="cart" ref = "cart">购物车</div>
  59. </div>
  60. </body>
  61. </html>
  62. <script src="vue.min.js"></script>
  63. <script>
  64. let vm = new Vue({
  65. el: '#app',
  66. data: {
  67. currentIndex: -1, // 加入购物车的默认索引
  68. isShow: false,
  69. lists: [
  70. {
  71. id: 1,
  72. cover: 'http://www.javascript.cn/203853de0f7a753037.png'
  73. },
  74. {
  75. id: 2,
  76. cover: 'http://www.javascript.cn/202711f70eea987000.jpg'
  77. },
  78. {
  79. id: 3,
  80. cover: 'http://www.javascript.cn/211735f96b30549920.jpg'
  81. }
  82. ]
  83. },
  84. methods: {
  85. addCart(index) {
  86. // console.log('加入购物车',index);
  87. this.currentIndex = index
  88. this.isShow = true
  89. // console.log(this.currentIndex);
  90. },
  91. enter(el,done) { // el 表示当前的动画元素 done 表示动画执行完成后的回调函数
  92. // 确定点击元素 li 的位置 ref= "listArray" 专门用来获取 dom 元素
  93. // console.log(this.$refs.listArray,111);
  94. let oLi = this.$refs.listArray[this.currentIndex];
  95. // console.log(oLi,222);
  96. let { x, y } = oLi.getBoundingClientRect();//
  97. // 设置 span 动画元素的位置 与点击元素 li 位置一样
  98. // console.log(x,y);
  99. el.style.left = x + 'px';
  100. el.style.top = y + 'px';
  101. el.style.background = `url(${this.lists[this.currentIndex].cover})`;
  102. el.style.backgroundSize = '100% 100%'
  103. // 求出购物车的位置
  104. let Cart = this.$refs.cart
  105. let {x:a,y:b} = Cart.getBoundingClientRect()// x, y 重新命名 a,b
  106. // 动画元素到购物车移动的距离
  107. el.style.transform = `translate3d(${a - x}px,${b - y}px,0) scale(0)`;
  108. el.addEventListener('transitionend', done, false);
  109. },
  110. after() {
  111. this.isShow = false;
  112. }
  113. }
  114. })
  115. </script>