s-float-menu.vue 2.0 KB

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