s-float-menu.vue 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <template>
  2. <!-- 模态背景:展开时显示,点击后折叠 -->
  3. <view class="modal-bg" v-if="fabRef?.isShow" @click="handleCollapseFab"></view>
  4. <!-- 悬浮按钮 -->
  5. <uni-fab
  6. ref="fabRef"
  7. horizontal="right"
  8. vertical="bottom"
  9. :direction="state.direction"
  10. :pattern="state.pattern"
  11. :content="state.content"
  12. @trigger="handleOpenLink"
  13. />
  14. </template>
  15. <script setup>
  16. /**
  17. * 悬浮按钮
  18. */
  19. import sheep from '@/sheep';
  20. import { reactive, ref, unref } from 'vue';
  21. import { onBackPress } from '@dcloudio/uni-app';
  22. // 定义属性
  23. const props = defineProps({
  24. data: {
  25. type: Object,
  26. default() {},
  27. }
  28. })
  29. // 悬浮按钮配置: https://uniapp.dcloud.net.cn/component/uniui/uni-fab.html#fab-props
  30. const state = reactive({
  31. // 可选样式配置项
  32. pattern: [],
  33. // 展开菜单内容配置项
  34. content: [],
  35. // 展开菜单显示方式:horizontal-水平显示,vertical-垂直显示
  36. direction: '',
  37. });
  38. // 悬浮按钮引用
  39. const fabRef = ref(null);
  40. // 按钮方向
  41. state.direction = props.data.direction;
  42. props.data?.list.forEach((item) => {
  43. // 按钮文字
  44. const text = props.data?.showText ? item.text : ''
  45. // 生成内容配置项
  46. state.content.push({ iconPath: sheep.$url.cdn(item.imgUrl), url: item.url, text });
  47. // 生成样式配置项
  48. state.pattern.push({ color: item.textColor });
  49. });
  50. // 处理链接跳转
  51. function handleOpenLink(e) {
  52. sheep.$router.go(e.item.url);
  53. }
  54. // 折叠
  55. function handleCollapseFab() {
  56. if (unref(fabRef)?.isShow) {
  57. unref(fabRef)?.close();
  58. }
  59. }
  60. // 按返回值后,折叠悬浮按钮
  61. onBackPress(() => {
  62. if (unref(fabRef)?.isShow) {
  63. unref(fabRef)?.close();
  64. return true;
  65. }
  66. return false;
  67. });
  68. </script>
  69. <style lang="scss" scoped>
  70. /* 模态背景 */
  71. .modal-bg {
  72. position: fixed;
  73. left: 0;
  74. top: 0;
  75. z-index: 11;
  76. width: 100%;
  77. height: 100%;
  78. background-color: rgba(#000000, 0.4);
  79. }
  80. </style>