uni-drawer.vue 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. props: {
  36. /**
  37. * 显示模式(左、右),只在初始化生效
  38. */
  39. mode: {
  40. type: String,
  41. default: ''
  42. },
  43. /**
  44. * 蒙层显示状态
  45. */
  46. mask: {
  47. type: Boolean,
  48. default: true
  49. },
  50. /**
  51. * 遮罩是否可点击关闭
  52. */
  53. maskClick:{
  54. type: Boolean,
  55. default: true
  56. },
  57. /**
  58. * 抽屉宽度
  59. */
  60. width: {
  61. type: Number,
  62. default: 220
  63. }
  64. },
  65. data() {
  66. return {
  67. visibleSync: false,
  68. showDrawer: false,
  69. rightMode: false,
  70. watchTimer: null,
  71. drawerWidth: 220
  72. }
  73. },
  74. created() {
  75. // #ifndef APP-NVUE
  76. this.drawerWidth = this.width
  77. // #endif
  78. this.rightMode = this.mode === 'right'
  79. },
  80. methods: {
  81. clear(){},
  82. close(type) {
  83. // fixed by mehaotian 抽屉尚未完全关闭或遮罩禁止点击时不触发以下逻辑
  84. if((type === 'mask' && !this.maskClick) || !this.visibleSync) return
  85. this._change('showDrawer', 'visibleSync', false)
  86. },
  87. open() {
  88. // fixed by mehaotian 处理重复点击打开的事件
  89. if(this.visibleSync) return
  90. this._change('visibleSync', 'showDrawer', true)
  91. },
  92. _change(param1, param2, status) {
  93. this[param1] = status
  94. if (this.watchTimer) {
  95. clearTimeout(this.watchTimer)
  96. }
  97. this.watchTimer = setTimeout(() => {
  98. this[param2] = status
  99. this.$emit('change',status)
  100. }, status ? 50 : 300)
  101. }
  102. }
  103. }
  104. </script>
  105. <style lang="scss" scoped>
  106. // 抽屉宽度
  107. $drawer-width: 220px;
  108. .uni-drawer {
  109. /* #ifndef APP-NVUE */
  110. display: block;
  111. /* #endif */
  112. position: fixed;
  113. top: 0;
  114. left: 0;
  115. right: 0;
  116. bottom: 0;
  117. overflow: hidden;
  118. z-index: 999;
  119. }
  120. .uni-drawer__content {
  121. /* #ifndef APP-NVUE */
  122. display: block;
  123. /* #endif */
  124. position: absolute;
  125. top: 0;
  126. width: $drawer-width;
  127. bottom: 0;
  128. background-color: $uni-bg-color;
  129. transition: transform 0.3s ease;
  130. }
  131. .uni-drawer--left {
  132. left: 0;
  133. /* #ifdef APP-NVUE */
  134. transform: translateX(-$drawer-width);
  135. /* #endif */
  136. /* #ifndef APP-NVUE */
  137. transform: translateX(-100%);
  138. /* #endif */
  139. }
  140. .uni-drawer--right {
  141. right: 0;
  142. /* #ifdef APP-NVUE */
  143. transform: translateX($drawer-width);
  144. /* #endif */
  145. /* #ifndef APP-NVUE */
  146. transform: translateX(100%);
  147. /* #endif */
  148. }
  149. .uni-drawer__content--visible {
  150. transform: translateX(0px);
  151. }
  152. .uni-drawer__mask {
  153. /* #ifndef APP-NVUE */
  154. display: block;
  155. /* #endif */
  156. opacity: 0;
  157. position: absolute;
  158. top: 0;
  159. left: 0;
  160. bottom: 0;
  161. right: 0;
  162. background-color: $uni-bg-color-mask;
  163. transition: opacity 0.3s;
  164. }
  165. .uni-drawer__mask--visible {
  166. /* #ifndef APP-NVUE */
  167. display: block;
  168. /* #endif */
  169. opacity: 1;
  170. }
  171. </style>