Skip to content

Commit 08f49e5

Browse files
committed
feat: 添加 fab 悬浮按钮组件
1 parent 223d75d commit 08f49e5

4 files changed

Lines changed: 498 additions & 0 deletions

File tree

‎components/uni-fab/uni-fab.vue‎

Lines changed: 357 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,357 @@
1+
<template>
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>
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>
29+
<text class="text">{{ item.text }}</text>
30+
</view>
31+
<view v-if="flexDirectionEnd || horizontalRight" class="fab-item first"></view>
32+
</view>
33+
</view>
34+
</view>
35+
</template>
36+
37+
<script>
38+
export default {
39+
name: 'uni-fab',
40+
props: {
41+
pattern: {
42+
type: Object,
43+
default () {
44+
return {};
45+
}
46+
},
47+
horizontal: {
48+
type: String,
49+
default: 'left'
50+
},
51+
vertical: {
52+
type: String,
53+
default: 'bottom'
54+
},
55+
direction: {
56+
type: String,
57+
default: 'horizontal'
58+
},
59+
content: {
60+
type: Array,
61+
default () {
62+
return [];
63+
}
64+
},
65+
show: {
66+
type: Boolean,
67+
default: false
68+
}
69+
},
70+
data() {
71+
return {
72+
fabShow: false,
73+
flug: true,
74+
isShow: false,
75+
styles: {
76+
color: '#3c3e49',
77+
selectedColor: '#007AFF',
78+
backgroundColor: '#fff',
79+
buttonColor: '#3c3e49'
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+
}
130+
},
131+
computed: {
132+
contentWidth(e) {
133+
return uni.upx2px((this.content.length + 1) * 110 + 20) + 'px';
134+
},
135+
contentWidthMin() {
136+
return uni.upx2px(110) + 'px';
137+
},
138+
// 动态计算宽度
139+
boxWidth() {
140+
return this.getPosition(3, 'horizontal');
141+
},
142+
// 动态计算高度
143+
boxHeight() {
144+
return this.getPosition(3, 'vertical');
145+
},
146+
// 计算左下位置
147+
leftBottom() {
148+
return this.getPosition(0, 'left', 'bottom');
149+
},
150+
// 计算右下位置
151+
rightBottom() {
152+
return this.getPosition(0, 'right', 'bottom');
153+
},
154+
// 计算左上位置
155+
leftTop() {
156+
return this.getPosition(0, 'left', 'top');
157+
},
158+
rightTop() {
159+
return this.getPosition(0, 'right', 'top');
160+
},
161+
flexDirectionStart() {
162+
return this.getPosition(1, 'vertical', 'top');
163+
},
164+
flexDirectionEnd() {
165+
return this.getPosition(1, 'vertical', 'bottom');
166+
},
167+
horizontalLeft() {
168+
return this.getPosition(2, 'horizontal', 'left');
169+
},
170+
horizontalRight() {
171+
return this.getPosition(2, 'horizontal', 'right');
172+
}
173+
}
174+
};
175+
</script>
176+
177+
<style scoped>
178+
@font-face {
179+
font-family: uniicons;
180+
font-weight: normal;
181+
font-style: normal;
182+
src: url('https://img-cdn-qiniu.dcloud.net.cn/fonts/uni.ttf') format('truetype');
183+
}
184+
185+
.uni-icon {
186+
font-family: uniicons;
187+
font-size: 24px;
188+
font-weight: normal;
189+
font-style: normal;
190+
line-height: 1;
191+
display: inline-block;
192+
text-decoration: none;
193+
-webkit-font-smoothing: antialiased;
194+
}
195+
196+
.uni-icon-plusempty:before {
197+
content: '\e468';
198+
}
199+
200+
.fab-box {
201+
position: fixed;
202+
display: flex;
203+
justify-content: center;
204+
align-items: center;
205+
z-index: 2;
206+
}
207+
208+
.fab-box.top {
209+
width: 60upx;
210+
height: 60upx;
211+
right: 30upx;
212+
bottom: 60upx;
213+
border: 1px #5989b9 solid;
214+
background: #6699cc;
215+
border-radius: 10upx;
216+
color: #fff;
217+
transition: all 0.3;
218+
opacity: 0;
219+
}
220+
221+
.fab-box.active {
222+
opacity: 1;
223+
}
224+
225+
.fab-box.fab {
226+
z-index: 10;
227+
}
228+
229+
.fab-box.fab.leftBottom {
230+
left: 30upx;
231+
bottom: 60upx;
232+
}
233+
234+
.fab-box.fab.leftTop {
235+
left: 30upx;
236+
top: 80upx;
237+
/* #ifdef H5 */
238+
top: calc(80upx + var(--window-top));
239+
/* #endif */
240+
}
241+
242+
.fab-box.fab.rightBottom {
243+
right: 30upx;
244+
bottom: 60upx;
245+
}
246+
247+
.fab-box.fab.rightTop {
248+
right: 30upx;
249+
top: 80upx;
250+
/* #ifdef H5 */
251+
top: calc(80upx + var(--window-top));
252+
/* #endif */
253+
}
254+
255+
.fab-circle {
256+
display: flex;
257+
justify-content: center;
258+
align-items: center;
259+
position: absolute;
260+
width: 110upx;
261+
height: 110upx;
262+
background: #3c3e49;
263+
/* background: #5989b9; */
264+
border-radius: 50%;
265+
box-shadow: 0 0 5px 2px rgba(0, 0, 0, 0.2);
266+
z-index: 11;
267+
}
268+
269+
.fab-circle.left {
270+
left: 0;
271+
}
272+
273+
.fab-circle.right {
274+
right: 0;
275+
}
276+
277+
.fab-circle.top {
278+
top: 0;
279+
}
280+
281+
.fab-circle.bottom {
282+
bottom: 0;
283+
}
284+
285+
.fab-circle .uni-icon-plusempty {
286+
color: #ffffff;
287+
font-size: 80upx;
288+
transition: all 0.3s;
289+
font-weight: bold;
290+
}
291+
292+
.fab-circle .uni-icon-plusempty.active {
293+
transform: rotate(135deg);
294+
font-size: 80upx;
295+
}
296+
297+
.fab-content {
298+
background: #6699cc;
299+
box-sizing: border-box;
300+
display: flex;
301+
border-radius: 100upx;
302+
overflow: hidden;
303+
box-shadow: 0 0 5px 2px rgba(0, 0, 0, 0.1);
304+
transition: all 0.2s;
305+
width: 110upx;
306+
}
307+
308+
.fab-content.left {
309+
justify-content: flex-start;
310+
}
311+
312+
.fab-content.right {
313+
justify-content: flex-end;
314+
}
315+
316+
.fab-content.flexDirection {
317+
flex-direction: column;
318+
justify-content: flex-end;
319+
}
320+
321+
.fab-content.flexDirectionStart {
322+
flex-direction: column;
323+
justify-content: flex-start;
324+
}
325+
326+
.fab-content.flexDirectionEnd {
327+
flex-direction: column;
328+
justify-content: flex-end;
329+
}
330+
331+
.fab-content .fab-item {
332+
display: flex;
333+
flex-direction: column;
334+
justify-content: center;
335+
align-items: center;
336+
width: 110upx;
337+
height: 110upx;
338+
font-size: 24upx;
339+
color: #fff;
340+
opacity: 0;
341+
transition: opacity 0.2s;
342+
}
343+
344+
.fab-content .fab-item.active {
345+
opacity: 1;
346+
}
347+
348+
.fab-content .fab-item .content-image {
349+
width: 50upx;
350+
height: 50upx;
351+
margin-bottom: 5upx;
352+
}
353+
354+
.fab-content .fab-item.first {
355+
width: 110upx;
356+
}
357+
</style>

‎pages.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,6 +668,11 @@
668668
"style": {
669669
"navigationBarTitleText": "calendar 日历"
670670
}
671+
}, {
672+
"path": "fab/fab",
673+
"style": {
674+
"navigationBarTitleText": "fab 悬浮按钮"
675+
}
671676
}
672677
]
673678
},

0 commit comments

Comments
 (0)