uni-drawer.vue 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. <template>
  2. <view v-if="visibleSync" :class="{'uni-drawer--visible':showDrawer,'uni-drawer--right':rightMode}" class="uni-drawer" @touchmove.stop.prevent="moveHandle">
  3. <view class="uni-drawer__mask" @click="close" />
  4. <view class="uni-drawer__content">
  5. <slot />
  6. </view>
  7. </view>
  8. </template>
  9. <script>
  10. export default {
  11. name: 'UniDrawer',
  12. props: {
  13. /**
  14. * 显示状态
  15. */
  16. visible: {
  17. type: Boolean,
  18. default: false
  19. },
  20. /**
  21. * 显示模式(左、右),只在初始化生效
  22. */
  23. mode: {
  24. type: String,
  25. default: ''
  26. },
  27. /**
  28. * 蒙层显示状态
  29. */
  30. mask: {
  31. type: Boolean,
  32. default: true
  33. }
  34. },
  35. data() {
  36. return {
  37. visibleSync: false,
  38. showDrawer: false,
  39. rightMode: false,
  40. closeTimer: null,
  41. watchTimer: null
  42. }
  43. },
  44. watch: {
  45. visible(val) {
  46. clearTimeout(this.watchTimer)
  47. setTimeout(() => {
  48. this.showDrawer = val
  49. }, 100)
  50. if (this.visibleSync) {
  51. clearTimeout(this.closeTimer)
  52. }
  53. if (val) {
  54. this.visibleSync = val
  55. } else {
  56. this.watchTimer = setTimeout(() => {
  57. this.visibleSync = val
  58. }, 300)
  59. }
  60. }
  61. },
  62. created() {
  63. this.visibleSync = this.visible
  64. setTimeout(() => {
  65. this.showDrawer = this.visible
  66. }, 100)
  67. this.rightMode = this.mode === 'right'
  68. },
  69. methods: {
  70. close() {
  71. this.showDrawer = false
  72. this.closeTimer = setTimeout(() => {
  73. this.visibleSync = false
  74. this.$emit('close')
  75. }, 200)
  76. },
  77. moveHandle() {}
  78. }
  79. }
  80. </script>
  81. <style>
  82. @charset "UTF-8";
  83. .uni-drawer {
  84. display: block;
  85. position: fixed;
  86. top: 0;
  87. left: 0;
  88. right: 0;
  89. bottom: 0;
  90. overflow: hidden;
  91. visibility: hidden;
  92. z-index: 10001;
  93. height: 100%
  94. }
  95. .uni-drawer.uni-drawer--right .uni-drawer__content {
  96. left: auto;
  97. right: 0;
  98. transform: translatex(100%)
  99. }
  100. .uni-drawer.uni-drawer--visible {
  101. visibility: visible
  102. }
  103. .uni-drawer.uni-drawer--visible .uni-drawer__content {
  104. transform: translatex(0)
  105. }
  106. .uni-drawer.uni-drawer--visible .uni-drawer__mask {
  107. display: block;
  108. opacity: 1
  109. }
  110. .uni-drawer__mask {
  111. display: block;
  112. opacity: 0;
  113. position: absolute;
  114. top: 0;
  115. left: 0;
  116. width: 100%;
  117. height: 100%;
  118. background: rgba(0, 0, 0, .4);
  119. transition: opacity .3s
  120. }
  121. .uni-drawer__content {
  122. display: block;
  123. position: absolute;
  124. top: 0;
  125. left: 0;
  126. width: 61.8%;
  127. height: 100%;
  128. background: #fff;
  129. transition: all .3s ease-out;
  130. transform: translatex(-100%)
  131. }
  132. </style>