u-transition.vue 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <template>
  2. <view
  3. v-if="inited"
  4. class="u-transition"
  5. ref="u-transition"
  6. @tap="clickHandler"
  7. :class="classes"
  8. :style="[mergeStyle]"
  9. @touchmove="noop"
  10. >
  11. <slot />
  12. </view>
  13. </template>
  14. <script>
  15. import props from './props.js';
  16. import mpMixin from '../../libs/mixin/mpMixin.js';
  17. import mixin from '../../libs/mixin/mixin.js';
  18. // 组件的methods方法,由于内容较长,写在外部文件中通过mixin引入
  19. import transition from "./transition.js";
  20. /**
  21. * transition 动画组件
  22. * @description
  23. * @tutorial
  24. * @property {String} show 是否展示组件 (默认 false )
  25. * @property {String} mode 使用的动画模式 (默认 'fade' )
  26. * @property {String | Number} duration 动画的执行时间,单位ms (默认 '300' )
  27. * @property {String} timingFunction 使用的动画过渡函数 (默认 'ease-out' )
  28. * @property {Object} customStyle 自定义样式
  29. * @event {Function} before-enter 进入前触发
  30. * @event {Function} enter 进入中触发
  31. * @event {Function} after-enter 进入后触发
  32. * @event {Function} before-leave 离开前触发
  33. * @event {Function} leave 离开中触发
  34. * @event {Function} after-leave 离开后触发
  35. * @example
  36. */
  37. export default {
  38. name: 'u-transition',
  39. data() {
  40. return {
  41. inited: false, // 是否显示/隐藏组件
  42. viewStyle: {}, // 组件内部的样式
  43. status: '', // 记录组件动画的状态
  44. transitionEnded: false, // 组件是否结束的标记
  45. display: false, // 组件是否展示
  46. classes: '', // 应用的类名
  47. }
  48. },
  49. computed: {
  50. mergeStyle() {
  51. const { viewStyle, customStyle } = this
  52. return {
  53. // #ifndef APP-NVUE
  54. transitionDuration: `${this.duration}ms`,
  55. // display: `${this.display ? '' : 'none'}`,
  56. transitionTimingFunction: this.timingFunction,
  57. // #endif
  58. // 避免自定义样式影响到动画属性,所以写在viewStyle前面
  59. ...uni.$u.addStyle(customStyle),
  60. ...viewStyle
  61. }
  62. }
  63. },
  64. // 将mixin挂在到组件中,uni.$u.mixin实际上为一个vue格式对象
  65. mixins: [mpMixin, mixin, transition, props],
  66. watch: {
  67. show: {
  68. handler(newVal) {
  69. // vue和nvue分别执行不同的方法
  70. // #ifdef APP-NVUE
  71. newVal ? this.nvueEnter() : this.nvueLeave()
  72. // #endif
  73. // #ifndef APP-NVUE
  74. newVal ? this.vueEnter() : this.vueLeave()
  75. // #endif
  76. },
  77. // 表示同时监听初始化时的props的show的意思
  78. immediate: true
  79. }
  80. }
  81. }
  82. </script>
  83. <style lang="scss" scoped>
  84. @import '../../libs/css/components.scss';
  85. /* #ifndef APP-NVUE */
  86. // vue版本动画相关的样式抽离在外部文件
  87. @import './vue.ani-style.scss';
  88. /* #endif */
  89. .u-transition {}
  90. </style>