绿满眶商城微信小程序-uniapp
zxl
2025-07-17 b303b6945a139153688e86635346a621fe0c29b3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
<template>
  <view class="fab-tool">
    <view id="toolfab">
      <slot></slot>
    </view>
    <view class="fab-tool-content" :style="placementStyle" id="placementfab">
      <slot name="content" v-if="visible"></slot>
    </view>
  </view>
</template>
 
<script>
export default {
  props: {
    visible: {
      type: Boolean,
      default: false
    },
    placement: {
      type: String,
      default: 'auto' // 'auto' | 'top-start' | 'top-center' | 'top-end' | 'bottom-start' | 'bottom-center' | 'bottom-end'
    }
  },
  data() {
    return {
      placementHeight: '0',
      placementType: ''
    }
  },
  watch: {
    visible(newVal) {
      if (newVal) {
        const { screenWidth } = uni.getSystemInfoSync()
 
        this.$nextTick(() => {
          let placementWidth = 0
          uni
            .createSelectorQuery()
            .in(this)
            .select('#placementfab')
            .boundingClientRect((res) => {
              this.placementHeight = -res.height + 'px'
              placementWidth = res.width
            })
            .exec()
          // 开启自动模式后
          if (this.placement == 'auto') {
            uni
              .createSelectorQuery()
              .in(this)
              .select('#toolfab')
              .boundingClientRect((res) => {
                let leftRemain = res.left
                let rightRemain = screenWidth - leftRemain
                if (rightRemain > placementWidth) {
                  this.placementType = 'bottom-start'
                } else if (leftRemain > placementWidth) {
                  this.placementType = 'bottom-end'
                } else {
                  this.placementType = 'bottom-center'
                }
              })
              .exec()
          }
        })
      }
    }
  },
  mounted() {
    this.placementType = this.placement
  },
  computed: {
    placementStyle() {
      let position = {}
      switch (this.placementType) {
        case 'top-start':
          position = {
            top: this.placementHeight,
            left: 0
          }
          break
        case 'top-center':
          position = {
            top: this.placementHeight,
            left: '50%',
            transform: 'translateX(-50%)'
          }
          break
        case 'top-end':
          position = {
            top: this.placementHeight,
            right: 0
          }
          break
        case 'bottom-start':
          position = {
            bottom: this.placementHeight,
            left: 0
          }
          break
        case 'bottom-center':
          position = {
            bottom: this.placementHeight,
            left: '50%',
            transform: 'translateX(-50%)'
          }
          break
        case 'bottom-end':
          position = {
            bottom: this.placementHeight,
            right: 0
          }
          break
        default:
          break
      }
      return position
    }
  },
  methods: {
    //
  }
}
</script>
 
<style lang="scss">
.fab-tool {
  position: relative;
 
  .fab-tool-content {
    position: absolute;
    z-index: 999;
 
    background-color: #ffffff;
    box-shadow: -2px -2px 4px rgba(0, 0, 0, 0.05), 2px 2px 4px rgba(0, 0, 0, 0.05);
    border-radius: 12rpx;
    box-sizing: border-box;
  }
}
</style>