uni-drawer.vue 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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}" :style="{width:drawerWidth+'px'}">
  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. * @property {Number} width 抽屉的宽度 ,仅 vue 页面生效
  20. * @event {Function} close 组件关闭时触发事件
  21. */
  22. export default {
  23. name: 'UniDrawer',
  24. props: {
  25. /**
  26. * 显示状态
  27. */
  28. visible: {
  29. type: Boolean,
  30. default: false
  31. },
  32. /**
  33. * 显示模式(左、右),只在初始化生效
  34. */
  35. mode: {
  36. type: String,
  37. default: ''
  38. },
  39. /**
  40. * 蒙层显示状态
  41. */
  42. mask: {
  43. type: Boolean,
  44. default: true
  45. },
  46. /**
  47. * 抽屉宽度
  48. */
  49. width: {
  50. type: Number,
  51. default: 220
  52. }
  53. },
  54. data() {
  55. return {
  56. visibleSync: false,
  57. showDrawer: false,
  58. rightMode: false,
  59. watchTimer: null,
  60. drawerWidth: 220
  61. }
  62. },
  63. watch: {
  64. visible(val) {
  65. if (val) {
  66. this.open()
  67. } else {
  68. this.close()
  69. }
  70. }
  71. },
  72. created() {
  73. // #ifndef APP-NVUE
  74. this.drawerWidth = this.width
  75. // #endif
  76. this.visibleSync = this.visible
  77. setTimeout(() => {
  78. this.showDrawer = this.visible
  79. }, 100)
  80. this.rightMode = this.mode === 'right'
  81. },
  82. methods: {
  83. close() {
  84. this._change('showDrawer', 'visibleSync', false)
  85. },
  86. open() {
  87. this._change('visibleSync', 'showDrawer', true)
  88. },
  89. _change(param1, param2, status) {
  90. this[param1] = status
  91. if (this.watchTimer) {
  92. clearTimeout(this.watchTimer)
  93. }
  94. this.watchTimer = setTimeout(() => {
  95. this[param2] = status
  96. this.$emit(status ? 'open' : 'close')
  97. }, status ? 50 : 300)
  98. }
  99. }
  100. }
  101. </script>
  102. <style lang="scss" scoped>
  103. // 抽屉宽度
  104. $drawer-width: 220px;
  105. .uni-drawer {
  106. /* #ifndef APP-NVUE */
  107. display: block;
  108. /* #endif */
  109. position: fixed;
  110. top: 0;
  111. left: 0;
  112. right: 0;
  113. bottom: 0;
  114. overflow: hidden;
  115. z-index: 999;
  116. }
  117. .uni-drawer__content {
  118. /* #ifndef APP-NVUE */
  119. display: block;
  120. /* #endif */
  121. position: absolute;
  122. top: 0;
  123. width: $drawer-width;
  124. bottom: 0;
  125. background-color: $uni-bg-color;
  126. transition: transform 0.3s ease;
  127. }
  128. .uni-drawer--left {
  129. left: 0;
  130. /* #ifdef APP-NVUE */
  131. transform: translateX(-$drawer-width);
  132. /* #endif */
  133. /* #ifndef APP-NVUE */
  134. transform: translateX(-100%);
  135. /* #endif */
  136. }
  137. .uni-drawer--right {
  138. right: 0;
  139. /* #ifdef APP-NVUE */
  140. transform: translateX($drawer-width);
  141. /* #endif */
  142. /* #ifndef APP-NVUE */
  143. transform: translateX(100%);
  144. /* #endif */
  145. }
  146. .uni-drawer__content--visible {
  147. transform: translateX(0px);
  148. }
  149. .uni-drawer__mask {
  150. /* #ifndef APP-NVUE */
  151. display: block;
  152. /* #endif */
  153. opacity: 0;
  154. position: absolute;
  155. top: 0;
  156. left: 0;
  157. bottom: 0;
  158. right: 0;
  159. background-color: $uni-bg-color-mask;
  160. transition: opacity 0.3s;
  161. }
  162. .uni-drawer__mask--visible {
  163. /* #ifndef APP-NVUE */
  164. display: block;
  165. /* #endif */
  166. opacity: 1;
  167. }
  168. </style>