uni-drawer.vue 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <template>
  2. <view v-if="visibleSync" :class="{ 'uni-drawer--visible': showDrawer }" class="uni-drawer">
  3. <view class="uni-drawer__mask" :class="{ 'uni-drawer__mask--visible': showDrawer && mask }" @tap="close" />
  4. <view class="uni-drawer__content" :class="{'uni-drawer--right': rightMode,'uni-drawer--left': !rightMode, 'uni-drawer__content--visible': showDrawer}">
  5. <slot />
  6. </view>
  7. </view>
  8. </template>
  9. <script>
  10. /**
  11. * Drawer 抽屉
  12. * @description 抽屉侧滑菜单
  13. * @tutorial https://ext.dcloud.net.cn/plugin?id=26
  14. * @property {Boolean} visible = [true|false] Drawer的显示状态
  15. * @property {Boolean} mask = [true | false] 是否显示遮罩
  16. * @property {Boolean} mode = [left | right] Drawer 滑出位置
  17. * @value left 从左侧滑出
  18. * @value right 从右侧侧滑出
  19. * @event {Function} close 组件关闭时触发事件
  20. */
  21. export default {
  22. name: 'UniDrawer',
  23. props: {
  24. /**
  25. * 显示状态
  26. */
  27. visible: {
  28. type: Boolean,
  29. default: false
  30. },
  31. /**
  32. * 显示模式(左、右),只在初始化生效
  33. */
  34. mode: {
  35. type: String,
  36. default: ''
  37. },
  38. /**
  39. * 蒙层显示状态
  40. */
  41. mask: {
  42. type: Boolean,
  43. default: true
  44. }
  45. },
  46. data() {
  47. return {
  48. visibleSync: false,
  49. showDrawer: false,
  50. rightMode: false,
  51. watchTimer: null
  52. }
  53. },
  54. watch: {
  55. visible(val) {
  56. if (val) {
  57. this.open()
  58. } else {
  59. this.close()
  60. }
  61. }
  62. },
  63. created() {
  64. this.visibleSync = this.visible
  65. setTimeout(() => {
  66. this.showDrawer = this.visible
  67. }, 100)
  68. this.rightMode = this.mode === 'right'
  69. },
  70. methods: {
  71. close() {
  72. this._change('showDrawer', 'visibleSync', false)
  73. },
  74. open() {
  75. this._change('visibleSync', 'showDrawer', true)
  76. },
  77. _change(param1, param2, status) {
  78. this[param1] = status
  79. if (this.watchTimer) {
  80. clearTimeout(this.watchTimer)
  81. }
  82. this.watchTimer = setTimeout(() => {
  83. this[param2] = status
  84. this.$emit(status ? 'open' : 'close')
  85. }, status ? 50 : 300)
  86. }
  87. }
  88. }
  89. </script>
  90. <style scoped>
  91. /* 抽屉宽度
  92. */
  93. .uni-drawer {
  94. /* #ifndef APP-NVUE */
  95. display: block;
  96. /* #endif */
  97. position: fixed;
  98. top: 0;
  99. left: 0;
  100. right: 0;
  101. bottom: 0;
  102. overflow: hidden;
  103. z-index: 999;
  104. }
  105. .uni-drawer__content {
  106. /* #ifndef APP-NVUE */
  107. display: block;
  108. /* #endif */
  109. position: absolute;
  110. top: 0;
  111. width: 220px;
  112. bottom: 0;
  113. background-color: #ffffff;
  114. transition: transform 0.3s ease;
  115. }
  116. .uni-drawer--left {
  117. left: 0;
  118. transform: translateX(-220px);
  119. }
  120. .uni-drawer--right {
  121. right: 0;
  122. transform: translateX(220px);
  123. }
  124. .uni-drawer__content--visible {
  125. transform: translateX(0px);
  126. }
  127. .uni-drawer__mask {
  128. /* #ifndef APP-NVUE */
  129. display: block;
  130. /* #endif */
  131. opacity: 0;
  132. position: absolute;
  133. top: 0;
  134. left: 0;
  135. bottom: 0;
  136. right: 0;
  137. background-color: rgba(0, 0, 0, 0.4);
  138. transition: opacity 0.3s;
  139. }
  140. .uni-drawer__mask--visible {
  141. /* #ifndef APP-NVUE */
  142. display: block;
  143. /* #endif */
  144. opacity: 1;
  145. }
  146. </style>