Skip to content

Commit 7af3e5a

Browse files
committed
添加 fab button 组件
1 parent 354efd8 commit 7af3e5a

4 files changed

Lines changed: 493 additions & 0 deletions

File tree

‎components/uni-fab.vue‎

Lines changed: 368 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,368 @@
1+
<template>
2+
<view>
3+
<view
4+
class="fab-box fab"
5+
:class="{
6+
leftBottom: leftBottom,
7+
rightBottom: rightBottom,
8+
leftTop: leftTop,
9+
rightTop: rightTop
10+
}"
11+
>
12+
<view
13+
class="fab-circle"
14+
:class="{
15+
left: horizontal === 'left' && direction === 'horizontal',
16+
top: vertical === 'top' && direction === 'vertical',
17+
bottom: vertical === 'bottom' && direction === 'vertical',
18+
right: horizontal === 'right' && direction === 'horizontal'
19+
}"
20+
:style="{ 'background-color': styles.buttonColor }"
21+
@click="open"
22+
>
23+
<text class="icon icon-jia" :class="{ active: showContent }"></text>
24+
</view>
25+
<view
26+
class="fab-content"
27+
:class="{
28+
left: horizontal === 'left',
29+
right: horizontal === 'right',
30+
flexDirection: direction === 'vertical',
31+
flexDirectionStart: flexDirectionStart,
32+
flexDirectionEnd: flexDirectionEnd
33+
}"
34+
:style="{ width: boxWidth, height: boxHeight, background: styles.backgroundColor }"
35+
>
36+
<view v-if="flexDirectionStart || horizontalLeft" class="fab-item first"></view>
37+
<view
38+
class="fab-item"
39+
v-for="(item, index) in content"
40+
:key="index"
41+
:class="{ active: showContent }"
42+
:style="{
43+
color: item.active ? styles.selectedColor : styles.color
44+
}"
45+
@click="taps(index, item)"
46+
>
47+
<image
48+
class="content-image"
49+
:src="item.active ? item.selectedIconPath : item.iconPath"
50+
mode="widthFix"
51+
></image>
52+
<text class="text">{{ item.text }}</text>
53+
</view>
54+
<view v-if="flexDirectionEnd || horizontalRight" class="fab-item first"></view>
55+
</view>
56+
</view>
57+
</view>
58+
</template>
59+
60+
<script>
61+
export default {
62+
props: {
63+
pattern: {
64+
type: Object,
65+
default: () => {
66+
return {};
67+
}
68+
},
69+
horizontal: {
70+
type: String,
71+
default: 'left'
72+
},
73+
vertical: {
74+
type: String,
75+
default: 'bottom'
76+
},
77+
direction: {
78+
type: String,
79+
default: 'horizontal'
80+
},
81+
content: {
82+
type: Array,
83+
default: () => {
84+
return [];
85+
}
86+
}
87+
},
88+
data() {
89+
return {
90+
fabShow: false,
91+
flug: true,
92+
showContent: false,
93+
styles: {
94+
color: '#3c3e49',
95+
selectedColor: '#007AFF',
96+
backgroundColor: '#fff',
97+
buttonColor: '#3c3e49'
98+
}
99+
};
100+
},
101+
created() {
102+
if (this.top === 0) {
103+
this.fabShow = true;
104+
}
105+
// 初始化样式
106+
this.styles = Object.assign({}, this.styles, this.pattern);
107+
},
108+
methods: {
109+
open() {
110+
this.showContent = !this.showContent;
111+
},
112+
/**
113+
* 按钮点击事件
114+
*/
115+
taps(index, item) {
116+
this.$emit('trigger', {
117+
index,
118+
item
119+
});
120+
},
121+
/**
122+
* 获取 位置信息
123+
*/
124+
getPosition(types, paramA, paramB) {
125+
if (types === 0) {
126+
return this.horizontal === paramA && this.vertical === paramB;
127+
} else if (types === 1) {
128+
return this.direction === paramA && this.vertical === paramB;
129+
} else if (types === 2) {
130+
return this.direction === paramA && this.horizontal === paramB;
131+
} else {
132+
return this.showContent && this.direction === paramA
133+
? this.contentWidth
134+
: this.contentWidthMin;
135+
}
136+
}
137+
},
138+
watch: {
139+
pattern(newValue, oldValue) {
140+
console.log(JSON.stringify(newValue));
141+
this.styles = Object.assign({}, this.styles, newValue);
142+
}
143+
},
144+
computed: {
145+
contentWidth(e) {
146+
return uni.upx2px((this.content.length + 1) * 110 + 20) + 'px';
147+
},
148+
contentWidthMin() {
149+
return uni.upx2px(110) + 'px';
150+
},
151+
// 动态计算宽度
152+
boxWidth() {
153+
return this.getPosition(3, 'horizontal');
154+
},
155+
// 动态计算高度
156+
boxHeight() {
157+
return this.getPosition(3, 'vertical');
158+
},
159+
// 计算左下位置
160+
leftBottom() {
161+
return this.getPosition(0, 'left', 'bottom');
162+
},
163+
// 计算右下位置
164+
rightBottom() {
165+
return this.getPosition(0, 'right', 'bottom');
166+
},
167+
// 计算左上位置
168+
leftTop() {
169+
return this.getPosition(0, 'left', 'top');
170+
},
171+
rightTop() {
172+
return this.getPosition(0, 'right', 'top');
173+
},
174+
flexDirectionStart() {
175+
return this.getPosition(1, 'vertical', 'top');
176+
},
177+
flexDirectionEnd() {
178+
return this.getPosition(1, 'vertical', 'bottom');
179+
},
180+
horizontalLeft() {
181+
return this.getPosition(2, 'horizontal', 'left');
182+
},
183+
horizontalRight() {
184+
return this.getPosition(2, 'horizontal', 'right');
185+
}
186+
}
187+
};
188+
</script>
189+
190+
<style scoped>
191+
.fab-box {
192+
position: fixed;
193+
display: flex;
194+
justify-content: center;
195+
align-items: center;
196+
z-index: 2;
197+
}
198+
199+
.fab-box.top {
200+
width: 60upx;
201+
height: 60upx;
202+
right: 30upx;
203+
bottom: 60upx;
204+
border: 1px #5989b9 solid;
205+
background: #6699cc;
206+
border-radius: 10upx;
207+
color: #fff;
208+
transition: all 0.3;
209+
opacity: 0;
210+
}
211+
212+
.fab-box.active {
213+
opacity: 1;
214+
}
215+
216+
.fab-box.fab {
217+
z-index: 10;
218+
}
219+
220+
.fab-box.fab.leftBottom {
221+
left: 30upx;
222+
bottom: 60upx;
223+
}
224+
225+
.fab-box.fab.leftTop {
226+
left: 30upx;
227+
top: 80upx;
228+
/* #ifdef H5 */
229+
top: calc(80upx + var(--window-top));
230+
/* #endif */
231+
}
232+
233+
.fab-box.fab.rightBottom {
234+
right: 30upx;
235+
bottom: 60upx;
236+
}
237+
238+
.fab-box.fab.rightTop {
239+
right: 30upx;
240+
top: 80upx;
241+
/* #ifdef H5 */
242+
top: calc(80upx + var(--window-top));
243+
/* #endif */
244+
}
245+
246+
.fab-circle {
247+
display: flex;
248+
justify-content: center;
249+
align-items: center;
250+
position: absolute;
251+
width: 110upx;
252+
height: 110upx;
253+
background: #3c3e49;
254+
/* background: #5989b9; */
255+
border-radius: 50%;
256+
box-shadow: 0 0 5px 2px rgba(0, 0, 0, 0.2);
257+
z-index: 11;
258+
}
259+
260+
.fab-circle.left {
261+
left: 0;
262+
}
263+
264+
.fab-circle.right {
265+
right: 0;
266+
}
267+
268+
.fab-circle.top {
269+
top: 0;
270+
}
271+
272+
.fab-circle.bottom {
273+
bottom: 0;
274+
}
275+
276+
.fab-circle .icon-jia {
277+
color: #ffffff;
278+
font-size: 50upx;
279+
transition: all 0.3s;
280+
}
281+
282+
.fab-circle .icon-jia.active {
283+
transform: rotate(135deg);
284+
}
285+
286+
.fab-content {
287+
background: #6699cc;
288+
box-sizing: border-box;
289+
display: flex;
290+
border-radius: 100upx;
291+
overflow: hidden;
292+
box-shadow: 0 0 5px 2px rgba(0, 0, 0, 0.1);
293+
transition: all 0.2s;
294+
width: 110upx;
295+
}
296+
297+
.fab-content.left {
298+
justify-content: flex-start;
299+
}
300+
301+
.fab-content.right {
302+
justify-content: flex-end;
303+
}
304+
305+
.fab-content.flexDirection {
306+
flex-direction: column;
307+
justify-content: flex-end;
308+
}
309+
310+
.fab-content.flexDirectionStart {
311+
flex-direction: column;
312+
justify-content: flex-start;
313+
}
314+
315+
.fab-content.flexDirectionEnd {
316+
flex-direction: column;
317+
justify-content: flex-end;
318+
}
319+
320+
.fab-content .fab-item {
321+
display: flex;
322+
flex-direction: column;
323+
justify-content: center;
324+
align-items: center;
325+
width: 110upx;
326+
height: 110upx;
327+
font-size: 24upx;
328+
color: #fff;
329+
opacity: 0;
330+
transition: opacity 0.2s;
331+
}
332+
333+
.fab-content .fab-item.active {
334+
opacity: 1;
335+
}
336+
337+
.fab-content .fab-item .content-image {
338+
width: 50upx;
339+
height: 50upx;
340+
margin-bottom: 5upx;
341+
}
342+
343+
.fab-content .fab-item.first {
344+
width: 110upx;
345+
}
346+
347+
@font-face {
348+
font-family: 'iconfont';
349+
src: url('https://at.alicdn.com/t/font_1028200_xhbo4rn58rp.ttf?t=1548214263520')
350+
format('truetype');
351+
}
352+
353+
.icon {
354+
font-family: 'iconfont' !important;
355+
font-size: 16px;
356+
font-style: normal;
357+
-webkit-font-smoothing: antialiased;
358+
-moz-osx-font-smoothing: grayscale;
359+
}
360+
361+
.icon-jia:before {
362+
content: '\e630';
363+
}
364+
365+
.icon-arrow-up:before {
366+
content: '\e603';
367+
}
368+
</style>

‎pages.json‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -898,6 +898,11 @@
898898
"style" : {
899899
"navigationBarTitleText" : "标签tag"
900900
}
901+
},{
902+
"path" : "fab/fab",
903+
"style" : {
904+
"navigationBarTitleText" : "悬浮按钮 fab"
905+
}
901906
},
902907
{
903908
"path" : "countdown/countdown",

‎pages/tabBar/template/template.vue‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,9 @@
171171
}, {
172172
name: '标签 tag',
173173
url: 'tag'
174+
}, {
175+
name: '悬浮按钮 fab',
176+
url: 'fab'
174177
},
175178
// #ifndef MP-ALIPAY || MP-TOUTIAO
176179
{

0 commit comments

Comments
 (0)