transition.js 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // 定义一个一定时间后自动成功的promise,让调用nextTick方法处,进入下一个then方法
  2. const nextTick = () => new Promise(resolve => setTimeout(resolve, 1000 / 50))
  3. // nvue动画模块实现细节抽离在外部文件
  4. // #ifdef APP-NVUE
  5. import animationMap from './nvue-ani-map.js'
  6. // #endif
  7. // #ifndef APP-NVUE
  8. // 定义类名,通过给元素动态切换类名,赋予元素一定的css动画样式
  9. const getClassNames = (name) => ({
  10. enter: `u-${name}-enter u-${name}-enter-active`,
  11. 'enter-to': `u-${name}-enter-to u-${name}-enter-active`,
  12. leave: `u-${name}-leave u-${name}-leave-active`,
  13. 'leave-to': `u-${name}-leave-to u-${name}-leave-active`
  14. })
  15. // #endif
  16. // #ifdef APP-NVUE
  17. // 引入nvue(weex)的animation动画模块,文档见:
  18. // https://weex.apache.org/zh/docs/modules/animation.html#transition
  19. const animation = uni.requireNativePlugin('animation')
  20. const getStyle = (name) => animationMap[name]
  21. // #endif
  22. export default {
  23. methods: {
  24. // 组件被点击发出事件
  25. clickHandler() {
  26. this.$emit('click')
  27. },
  28. // #ifndef APP-NVUE
  29. // vue版本的组件进场处理
  30. vueEnter() {
  31. // 动画进入时的类名
  32. const classNames = getClassNames(this.mode)
  33. // 定义状态和发出动画进入前事件
  34. this.status = 'enter'
  35. this.$emit('beforeEnter')
  36. this.inited = true
  37. this.display = true
  38. this.classes = classNames.enter
  39. this.$nextTick(async () => {
  40. // #ifdef H5
  41. await uni.$u.sleep(20)
  42. // #endif
  43. // 标识动画尚未结束
  44. this.$emit('enter')
  45. this.transitionEnded = false
  46. // 组件动画进入后触发的事件
  47. this.$emit('afterEnter')
  48. // 赋予组件enter-to类名
  49. this.classes = classNames['enter-to']
  50. })
  51. },
  52. // 动画离场处理
  53. vueLeave() {
  54. // 如果不是展示状态,无需执行逻辑
  55. if (!this.display) return
  56. const classNames = getClassNames(this.mode)
  57. // 标记离开状态和发出事件
  58. this.status = 'leave'
  59. this.$emit('beforeLeave')
  60. // 获得类名
  61. this.classes = classNames.leave
  62. this.$nextTick(() => {
  63. // 动画正在离场的状态
  64. this.transitionEnded = false
  65. this.$emit('leave')
  66. // 组件执行动画,到了执行的执行时间后,执行一些额外处理
  67. setTimeout(this.onTransitionEnd, this.duration)
  68. this.classes = classNames['leave-to']
  69. })
  70. },
  71. // #endif
  72. // #ifdef APP-NVUE
  73. // nvue版本动画进场
  74. nvueEnter() {
  75. // 获得样式的名称
  76. const currentStyle = getStyle(this.mode)
  77. // 组件动画状态和发出事件
  78. this.status = 'enter'
  79. this.$emit('beforeEnter')
  80. // 展示生成组件元素
  81. this.inited = true
  82. this.display = true
  83. // 在nvue安卓上,由于渲染速度慢,在弹窗,键盘,日历等组件中,渲染其中的内容需要时间
  84. // 导致出现弹窗卡顿,这里让其一开始为透明状态,等一定时间渲染完成后,再让其隐藏起来,再让其按正常逻辑出现
  85. this.viewStyle = {
  86. opacity: 0
  87. }
  88. // 等待弹窗内容渲染完成
  89. this.$nextTick(() => {
  90. // 合并样式
  91. this.viewStyle = currentStyle.enter
  92. Promise.resolve()
  93. .then(nextTick)
  94. .then(() => {
  95. // 组件开始进入前的事件
  96. this.$emit('enter')
  97. // nvue的transition动画模块需要通过ref调用组件,注意此处的ref不同于vue的this.$refs['u-transition']用法
  98. animation.transition(this.$refs['u-transition'].ref, {
  99. styles: currentStyle['enter-to'],
  100. duration: this.duration,
  101. timingFunction: this.timingFunction,
  102. needLayout: false,
  103. delay: 0
  104. }, () => {
  105. // 动画执行完毕,发出事件
  106. this.$emit('afterEnter')
  107. })
  108. })
  109. .catch(() => {})
  110. })
  111. },
  112. nvueLeave() {
  113. if (!this.display) {
  114. return
  115. }
  116. const currentStyle = getStyle(this.mode)
  117. // 定义状态和事件
  118. this.status = 'leave'
  119. this.$emit('beforeLeave')
  120. // 合并样式
  121. this.viewStyle = currentStyle.leave
  122. // 放到promise中处理执行过程
  123. Promise.resolve()
  124. .then(nextTick) // 等待几十ms
  125. .then(() => {
  126. this.transitionEnded = false
  127. // 动画正在离场的状态
  128. this.$emit('leave')
  129. animation.transition(this.$refs['u-transition'].ref, {
  130. styles: currentStyle['leave-to'],
  131. duration: this.duration,
  132. timingFunction: this.timingFunction,
  133. needLayout: false,
  134. delay: 0
  135. }, () => {
  136. this.onTransitionEnd()
  137. })
  138. })
  139. .catch(() => {})
  140. },
  141. // #endif
  142. // 完成过渡后触发
  143. onTransitionEnd() {
  144. // 如果已经是结束的状态,无需再处理
  145. if (this.transitionEnded) return
  146. this.transitionEnded = true
  147. // 发出组件动画执行后的事件
  148. this.$emit(this.status === 'leave' ? 'afterLeave' : 'afterEnter')
  149. if (!this.show && this.display) {
  150. this.display = false
  151. this.inited = false
  152. }
  153. }
  154. }
  155. }