|
1 | 1 | <template> |
2 | 2 | <view> |
3 | | - <view class="fab-box fab" :class="{ |
4 | | - leftBottom: leftBottom, |
5 | | - rightBottom: rightBottom, |
6 | | - leftTop: leftTop, |
7 | | - rightTop: rightTop |
8 | | - }"> |
9 | | - <view class="fab-circle" :class="{ |
10 | | - left: horizontal === 'left' && direction === 'horizontal', |
11 | | - top: vertical === 'top' && direction === 'vertical', |
12 | | - bottom: vertical === 'bottom' && direction === 'vertical', |
13 | | - right: horizontal === 'right' && direction === 'horizontal' |
14 | | - }" :style="{ 'background-color': styles.buttonColor }" @click="_onClick"> |
15 | | - <text class="uni-icon uni-icon-plusempty" :class="{ active: isShow }"></text> |
| 3 | + <view :class="{ |
| 4 | + leftBottom: leftBottom, |
| 5 | + rightBottom: rightBottom, |
| 6 | + leftTop: leftTop, |
| 7 | + rightTop: rightTop |
| 8 | + }" class="fab-box fab"> |
| 9 | + <view :class="{ |
| 10 | + left: horizontal === 'left' && direction === 'horizontal', |
| 11 | + top: vertical === 'top' && direction === 'vertical', |
| 12 | + bottom: vertical === 'bottom' && direction === 'vertical', |
| 13 | + right: horizontal === 'right' && direction === 'horizontal' |
| 14 | + }" :style="{ 'background-color': styles.buttonColor }" class="fab-circle" @click="_onClick"> |
| 15 | + <text :class="{ active: isShow }" class="uni-icon uni-icon-plusempty" /> |
16 | 16 | </view> |
17 | | - <view class="fab-content" :class="{ |
18 | | - left: horizontal === 'left', |
19 | | - right: horizontal === 'right', |
20 | | - flexDirection: direction === 'vertical', |
21 | | - flexDirectionStart: flexDirectionStart, |
22 | | - flexDirectionEnd: flexDirectionEnd |
23 | | - }" :style="{ width: boxWidth, height: boxHeight, background: styles.backgroundColor }"> |
24 | | - <view v-if="flexDirectionStart || horizontalLeft" class="fab-item first"></view> |
25 | | - <view class="fab-item" v-for="(item, index) in content" :key="index" :class="{ active: isShow }" :style="{ |
26 | | - color: item.active ? styles.selectedColor : styles.color |
27 | | - }" @click="_onItemClick(index, item)"> |
28 | | - <image class="content-image" :src="item.active ? item.selectedIconPath : item.iconPath" mode="widthFix"></image> |
| 17 | + <view :class="{ |
| 18 | + left: horizontal === 'left', |
| 19 | + right: horizontal === 'right', |
| 20 | + flexDirection: direction === 'vertical', |
| 21 | + flexDirectionStart: flexDirectionStart, |
| 22 | + flexDirectionEnd: flexDirectionEnd |
| 23 | + }" :style="{ width: boxWidth, height: boxHeight, background: styles.backgroundColor }" class="fab-content"> |
| 24 | + <view v-if="flexDirectionStart || horizontalLeft" class="fab-item first" /> |
| 25 | + <view v-for="(item, index) in content" :key="index" :class="{ active: isShow }" :style="{ |
| 26 | + color: item.active ? styles.selectedColor : styles.color |
| 27 | + }" class="fab-item" @click="_onItemClick(index, item)"> |
| 28 | + <image :src="item.active ? item.selectedIconPath : item.iconPath" class="content-image" mode="widthFix" /> |
29 | 29 | <text class="text">{{ item.text }}</text> |
30 | 30 | </view> |
31 | | - <view v-if="flexDirectionEnd || horizontalRight" class="fab-item first"></view> |
| 31 | + <view v-if="flexDirectionEnd || horizontalRight" class="fab-item first" /> |
32 | 32 | </view> |
33 | 33 | </view> |
34 | 34 | </view> |
35 | 35 | </template> |
36 | 36 |
|
37 | 37 | <script> |
38 | 38 | export default { |
39 | | - name: 'uni-fab', |
| 39 | + name: 'UniFab', |
40 | 40 | props: { |
41 | 41 | pattern: { |
42 | 42 | type: Object, |
43 | 43 | default () { |
44 | | - return {}; |
| 44 | + return {} |
45 | 45 | } |
46 | 46 | }, |
47 | 47 | horizontal: { |
|
59 | 59 | content: { |
60 | 60 | type: Array, |
61 | 61 | default () { |
62 | | - return []; |
| 62 | + return [] |
63 | 63 | } |
64 | 64 | }, |
65 | 65 | show: { |
|
78 | 78 | backgroundColor: '#fff', |
79 | 79 | buttonColor: '#3c3e49' |
80 | 80 | } |
81 | | - }; |
82 | | - }, |
83 | | - created() { |
84 | | - this.isShow = this.show; |
85 | | - if (this.top === 0) { |
86 | | - this.fabShow = true; |
87 | | - } |
88 | | - // 初始化样式 |
89 | | - this.styles = Object.assign({}, this.styles, this.pattern); |
90 | | - }, |
91 | | - methods: { |
92 | | - _onClick() { |
93 | | - this.isShow = !this.isShow; |
94 | | - }, |
95 | | - open() { |
96 | | - this.isShow = true; |
97 | | - }, |
98 | | - close() { |
99 | | - this.isShow = false; |
100 | | - }, |
101 | | - /** |
102 | | - * 按钮点击事件 |
103 | | - */ |
104 | | - _onItemClick(index, item) { |
105 | | - this.$emit('trigger', { |
106 | | - index, |
107 | | - item |
108 | | - }); |
109 | | - }, |
110 | | - /** |
111 | | - * 获取 位置信息 |
112 | | - */ |
113 | | - getPosition(types, paramA, paramB) { |
114 | | - if (types === 0) { |
115 | | - return this.horizontal === paramA && this.vertical === paramB; |
116 | | - } else if (types === 1) { |
117 | | - return this.direction === paramA && this.vertical === paramB; |
118 | | - } else if (types === 2) { |
119 | | - return this.direction === paramA && this.horizontal === paramB; |
120 | | - } else { |
121 | | - return this.isShow && this.direction === paramA ? this.contentWidth : this.contentWidthMin; |
122 | | - } |
123 | | - } |
124 | | - }, |
125 | | - watch: { |
126 | | - pattern(newValue, oldValue) { |
127 | | - console.log(JSON.stringify(newValue)); |
128 | | - this.styles = Object.assign({}, this.styles, newValue); |
129 | 81 | } |
130 | 82 | }, |
131 | 83 | computed: { |
132 | 84 | contentWidth(e) { |
133 | | - return uni.upx2px((this.content.length + 1) * 110 + 20) + 'px'; |
| 85 | + return uni.upx2px((this.content.length + 1) * 110 + 20) + 'px' |
134 | 86 | }, |
135 | 87 | contentWidthMin() { |
136 | | - return uni.upx2px(110) + 'px'; |
| 88 | + return uni.upx2px(110) + 'px' |
137 | 89 | }, |
138 | 90 | // 动态计算宽度 |
139 | 91 | boxWidth() { |
140 | | - return this.getPosition(3, 'horizontal'); |
| 92 | + return this.getPosition(3, 'horizontal') |
141 | 93 | }, |
142 | 94 | // 动态计算高度 |
143 | 95 | boxHeight() { |
144 | | - return this.getPosition(3, 'vertical'); |
| 96 | + return this.getPosition(3, 'vertical') |
145 | 97 | }, |
146 | 98 | // 计算左下位置 |
147 | 99 | leftBottom() { |
148 | | - return this.getPosition(0, 'left', 'bottom'); |
| 100 | + return this.getPosition(0, 'left', 'bottom') |
149 | 101 | }, |
150 | 102 | // 计算右下位置 |
151 | 103 | rightBottom() { |
152 | | - return this.getPosition(0, 'right', 'bottom'); |
| 104 | + return this.getPosition(0, 'right', 'bottom') |
153 | 105 | }, |
154 | 106 | // 计算左上位置 |
155 | 107 | leftTop() { |
156 | | - return this.getPosition(0, 'left', 'top'); |
| 108 | + return this.getPosition(0, 'left', 'top') |
157 | 109 | }, |
158 | 110 | rightTop() { |
159 | | - return this.getPosition(0, 'right', 'top'); |
| 111 | + return this.getPosition(0, 'right', 'top') |
160 | 112 | }, |
161 | 113 | flexDirectionStart() { |
162 | | - return this.getPosition(1, 'vertical', 'top'); |
| 114 | + return this.getPosition(1, 'vertical', 'top') |
163 | 115 | }, |
164 | 116 | flexDirectionEnd() { |
165 | | - return this.getPosition(1, 'vertical', 'bottom'); |
| 117 | + return this.getPosition(1, 'vertical', 'bottom') |
166 | 118 | }, |
167 | 119 | horizontalLeft() { |
168 | | - return this.getPosition(2, 'horizontal', 'left'); |
| 120 | + return this.getPosition(2, 'horizontal', 'left') |
169 | 121 | }, |
170 | 122 | horizontalRight() { |
171 | | - return this.getPosition(2, 'horizontal', 'right'); |
| 123 | + return this.getPosition(2, 'horizontal', 'right') |
| 124 | + } |
| 125 | + }, |
| 126 | + watch: { |
| 127 | + pattern(newValue, oldValue) { |
| 128 | + console.log(JSON.stringify(newValue)) |
| 129 | + this.styles = Object.assign({}, this.styles, newValue) |
| 130 | + } |
| 131 | + }, |
| 132 | + created() { |
| 133 | + this.isShow = this.show |
| 134 | + if (this.top === 0) { |
| 135 | + this.fabShow = true |
| 136 | + } |
| 137 | + // 初始化样式 |
| 138 | + this.styles = Object.assign({}, this.styles, this.pattern) |
| 139 | + }, |
| 140 | + methods: { |
| 141 | + _onClick() { |
| 142 | + this.isShow = !this.isShow |
| 143 | + }, |
| 144 | + open() { |
| 145 | + this.isShow = true |
| 146 | + }, |
| 147 | + close() { |
| 148 | + this.isShow = false |
| 149 | + }, |
| 150 | + /** |
| 151 | + * 按钮点击事件 |
| 152 | + */ |
| 153 | + _onItemClick(index, item) { |
| 154 | + this.$emit('trigger', { |
| 155 | + index, |
| 156 | + item |
| 157 | + }) |
| 158 | + }, |
| 159 | + /** |
| 160 | + * 获取 位置信息 |
| 161 | + */ |
| 162 | + getPosition(types, paramA, paramB) { |
| 163 | + if (types === 0) { |
| 164 | + return this.horizontal === paramA && this.vertical === paramB |
| 165 | + } else if (types === 1) { |
| 166 | + return this.direction === paramA && this.vertical === paramB |
| 167 | + } else if (types === 2) { |
| 168 | + return this.direction === paramA && this.horizontal === paramB |
| 169 | + } else { |
| 170 | + return this.isShow && this.direction === paramA ? this.contentWidth : this.contentWidthMin |
| 171 | + } |
172 | 172 | } |
173 | 173 | } |
174 | | - }; |
| 174 | + } |
175 | 175 | </script> |
176 | 176 |
|
177 | 177 | <style scoped> |
|
0 commit comments