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.

96 lines
2.8 KiB

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