s-float-menu.vue 1.9 KB

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