uni-drawer.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. <template>
  2. <view v-if="visibleSync" :class="{ 'uni-drawer--visible': showDrawer }" class="uni-drawer" @touchmove.stop.prevent="clear">
  3. <view class="uni-drawer__mask" :class="{ 'uni-drawer__mask--visible': showDrawer && mask }" @tap="close('mask')" />
  4. <view class="uni-drawer__content" :class="{'uni-drawer--right': rightMode,'uni-drawer--left': !rightMode, 'uni-drawer__content--visible': showDrawer}" :style="{width:drawerWidth+'px'}">
  5. <slot />
  6. </view>
  7. <!-- #ifdef H5 -->
  8. <keypress @esc="close('mask')" />
  9. <!-- #endif -->
  10. </view>
  11. </template>
  12. <script>
  13. // #ifdef H5
  14. import keypress from './keypress.js'
  15. // #endif
  16. /**
  17. * Drawer 抽屉
  18. * @description 抽屉侧滑菜单
  19. * @tutorial https://ext.dcloud.net.cn/plugin?id=26
  20. * @property {Boolean} mask = [true | false] 是否显示遮罩
  21. * @property {Boolean} maskClick = [true | false] 点击遮罩是否关闭
  22. * @property {Boolean} mode = [left | right] Drawer 滑出位置
  23. * @value left 从左侧滑出
  24. * @value right 从右侧侧滑出
  25. * @property {Number} width 抽屉的宽度 ,仅 vue 页面生效
  26. * @event {Function} close 组件关闭时触发事件
  27. */
  28. export default {
  29. name: 'UniDrawer',
  30. components: {
  31. // #ifdef H5
  32. keypress
  33. // #endif
  34. },
  35. emits:['change'],
  36. props: {
  37. /**
  38. * 显示模式(左、右),只在初始化生效
  39. */
  40. mode: {
  41. type: String,
  42. default: ''
  43. },
  44. /**
  45. * 蒙层显示状态
  46. */
  47. mask: {
  48. type: Boolean,
  49. default: true
  50. },
  51. /**
  52. * 遮罩是否可点击关闭
  53. */
  54. maskClick:{
  55. type: Boolean,
  56. default: true
  57. },
  58. /**
  59. * 抽屉宽度
  60. */
  61. width: {
  62. type: Number,
  63. default: 220
  64. }
  65. },
  66. data() {
  67. return {
  68. visibleSync: false,
  69. showDrawer: false,
  70. rightMode: false,
  71. watchTimer: null,
  72. drawerWidth: 220
  73. }
  74. },
  75. created() {
  76. // #ifndef APP-NVUE
  77. this.drawerWidth = this.width
  78. // #endif
  79. this.rightMode = this.mode === 'right'
  80. },
  81. methods: {
  82. clear(){},
  83. close(type) {
  84. // fixed by mehaotian 抽屉尚未完全关闭或遮罩禁止点击时不触发以下逻辑
  85. if((type === 'mask' && !this.maskClick) || !this.visibleSync) return
  86. this._change('showDrawer', 'visibleSync', false)
  87. },
  88. open() {
  89. // fixed by mehaotian 处理重复点击打开的事件
  90. if(this.visibleSync) return
  91. this._change('visibleSync', 'showDrawer', true)
  92. },
  93. _change(param1, param2, status) {
  94. this[param1] = status
  95. if (this.watchTimer) {
  96. clearTimeout(this.watchTimer)
  97. }
  98. this.watchTimer = setTimeout(() => {
  99. this[param2] = status
  100. this.$emit('change',status)
  101. }, status ? 50 : 300)
  102. }
  103. }
  104. }
  105. </script>
  106. <style lang="scss" scoped>
  107. $uni-mask: rgba($color: #000000, $alpha: 0.4) ;
  108. // 抽屉宽度
  109. $drawer-width: 220px;
  110. .uni-drawer {
  111. /* #ifndef APP-NVUE */
  112. display: block;
  113. /* #endif */
  114. position: fixed;
  115. top: 0;
  116. left: 0;
  117. right: 0;
  118. bottom: 0;
  119. overflow: hidden;
  120. z-index: 999;
  121. }
  122. .uni-drawer__content {
  123. /* #ifndef APP-NVUE */
  124. display: block;
  125. /* #endif */
  126. position: absolute;
  127. top: 0;
  128. width: $drawer-width;
  129. bottom: 0;
  130. background-color: $uni-bg-color;
  131. transition: transform 0.3s ease;
  132. }
  133. .uni-drawer--left {
  134. left: 0;
  135. /* #ifdef APP-NVUE */
  136. transform: translateX(-$drawer-width);
  137. /* #endif */
  138. /* #ifndef APP-NVUE */
  139. transform: translateX(-100%);
  140. /* #endif */
  141. }
  142. .uni-drawer--right {
  143. right: 0;
  144. /* #ifdef APP-NVUE */
  145. transform: translateX($drawer-width);
  146. /* #endif */
  147. /* #ifndef APP-NVUE */
  148. transform: translateX(100%);
  149. /* #endif */
  150. }
  151. .uni-drawer__content--visible {
  152. transform: translateX(0px);
  153. }
  154. .uni-drawer__mask {
  155. /* #ifndef APP-NVUE */
  156. display: block;
  157. /* #endif */
  158. opacity: 0;
  159. position: absolute;
  160. top: 0;
  161. left: 0;
  162. bottom: 0;
  163. right: 0;
  164. background-color: $uni-mask;
  165. transition: opacity 0.3s;
  166. }
  167. .uni-drawer__mask--visible {
  168. /* #ifndef APP-NVUE */
  169. display: block;
  170. /* #endif */
  171. opacity: 1;
  172. }
  173. </style>