You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
6.3 KiB

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