uni-drawer.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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" @tap="close" />
  4. <view class="uni-drawer__content" :class="{ 'safe-area': isIphoneX }"><slot /></view>
  5. </view>
  6. </template>
  7. <script>
  8. export default {
  9. name: 'UniDrawer',
  10. props: {
  11. /**
  12. * 显示状态
  13. */
  14. visible: {
  15. type: Boolean,
  16. default: false
  17. },
  18. /**
  19. * 显示模式(左、右),只在初始化生效
  20. */
  21. mode: {
  22. type: String,
  23. default: ''
  24. },
  25. /**
  26. * 蒙层显示状态
  27. */
  28. mask: {
  29. type: Boolean,
  30. default: true
  31. }
  32. },
  33. data() {
  34. return {
  35. visibleSync: false,
  36. showDrawer: false,
  37. rightMode: false,
  38. closeTimer: null,
  39. watchTimer: null,
  40. isIphoneX: false
  41. };
  42. },
  43. watch: {
  44. visible(val) {
  45. clearTimeout(this.watchTimer);
  46. setTimeout(() => {
  47. this.showDrawer = val;
  48. }, 100);
  49. if (this.visibleSync) {
  50. clearTimeout(this.closeTimer);
  51. }
  52. if (val) {
  53. this.visibleSync = val;
  54. } else {
  55. this.watchTimer = setTimeout(() => {
  56. this.visibleSync = val;
  57. }, 300);
  58. }
  59. }
  60. },
  61. created() {
  62. this.isIphoneX = this.$util.uniappIsIPhoneX();
  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: 999;
  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, 0.4);
  119. transition: opacity 0.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 0.3s ease-out;
  130. transform: translatex(-100%);
  131. }
  132. .safe-area {
  133. padding-bottom: 68rpx;
  134. padding-top: 44rpx;
  135. box-sizing: border-box;
  136. }
  137. </style>