drawer.vue 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <template>
  2. <view>
  3. <uni-card is-full :is-shadow="false">
  4. <text class="uni-h6">这是抽屉式导航组件使用示例,可以指定菜单左侧或者右侧弹出(仅初始化生效),组件内部可以放置任何内容。点击页面按钮即可显示导航菜单。</text>
  5. </uni-card>
  6. <uni-section title="左侧滑出" type="line">
  7. <view class="example-body">
  8. <button type="primary" @click="showDrawer('showLeft')"><text class="word-btn-white">显示Drawer</text>
  9. </button>
  10. <uni-drawer ref="showLeft" mode="left" :width="320" @change="change($event,'showLeft')">
  11. <view class="close">
  12. <button
  13. @click="closeDrawer('showLeft')"><text class="word-btn-white">关闭Drawer</text></button>
  14. </view>
  15. </uni-drawer>
  16. </view>
  17. </uni-section>
  18. <uni-section title="右侧滑出" type="line">
  19. <view class="example-body">
  20. <button type="primary" @click="showDrawer('showRight')"><text class="word-btn-white">显示Drawer</text>
  21. </button>
  22. <uni-drawer ref="showRight" mode="right" :mask-click="false" @change="change($event,'showRight')">
  23. <view class="scroll-view">
  24. <scroll-view class="scroll-view-box" scroll-y="true">
  25. <view class="info">
  26. <text class="info-text">右侧遮罩只能通过按钮关闭,不能通过点击遮罩关闭</text>
  27. </view>
  28. <view class="close">
  29. <button @click="closeDrawer('showRight')"><text
  30. class="word-btn-white">关闭Drawer</text></button>
  31. </view>
  32. <view class="info-content" v-for="item in 100" :key="item">
  33. <text>可滚动内容 {{item}}</text>
  34. </view>
  35. <view class="close">
  36. <button @click="closeDrawer('showRight')"><text
  37. class="word-btn-white">关闭Drawer</text></button>
  38. </view>
  39. </scroll-view>
  40. </view>
  41. </uni-drawer>
  42. </view>
  43. </uni-section>
  44. </view>
  45. </template>
  46. <script>
  47. export default {
  48. data() {
  49. return {
  50. showRight: false,
  51. showLeft: false
  52. }
  53. },
  54. methods: {
  55. confirm() {},
  56. // 打开窗口
  57. showDrawer(e) {
  58. this.$refs[e].open()
  59. },
  60. // 关闭窗口
  61. closeDrawer(e) {
  62. this.$refs[e].close()
  63. },
  64. // 抽屉状态发生变化触发
  65. change(e, type) {
  66. console.log((type === 'showLeft' ? '左窗口' : '右窗口') + (e ? '打开' : '关闭'));
  67. this[type] = e
  68. }
  69. },
  70. onNavigationBarButtonTap(e) {
  71. if (this.showLeft) {
  72. this.$refs.showLeft.close()
  73. } else {
  74. this.$refs.showLeft.open()
  75. }
  76. },
  77. // app端拦截返回事件 ,仅app端生效
  78. onBackPress() {
  79. if (this.showRight || this.showLeft) {
  80. this.$refs.showLeft.close()
  81. this.$refs.showRight.close()
  82. return true
  83. }
  84. }
  85. }
  86. </script>
  87. <style lang="scss">
  88. .example-body {
  89. padding: 10px;
  90. }
  91. .scroll-view {
  92. /* #ifndef APP-NVUE */
  93. width: 100%;
  94. height: 100%;
  95. /* #endif */
  96. flex:1
  97. }
  98. // 处理抽屉内容滚动
  99. .scroll-view-box {
  100. flex: 1;
  101. position: absolute;
  102. top: 0;
  103. right: 0;
  104. bottom: 0;
  105. left: 0;
  106. }
  107. .info {
  108. padding: 15px;
  109. color: #666;
  110. }
  111. .info-text {
  112. font-size: 14px;
  113. color: #666;
  114. }
  115. .info-content {
  116. padding: 5px 15px;
  117. }
  118. .close {
  119. padding: 10px;
  120. }
  121. </style>