ButtonGroup.vue
2.46 KB
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
<template>
<view class="button-group">
<view
v-for="(item, index) in selectList"
:key="index"
class="button-item"
:class="{ active: index === selectIndex }"
@click="chooseType(index, item)"
:style="{ width: itemWidth + 'rpx', marginRight: index === selectList.length - 1 ? 0 : gap + 'rpx' }"
>
<text class="label">{{ item[labelKey] }}</text>
<!-- 选中时的背景动画元素 -->
<view class="active-bg" v-if="index === selectIndex"></view>
</view>
</view>
</template>
<script type='text/ecmascript-6'>
export default {
props: {
selectList: {
type: Array,
default: () => []
},
labelKey: {
type: String,
default: 'label'
}
},
data() {
return {
selectIndex: 0,
itemWidth: 104, // 按钮固定宽度
gap: 16 // 按钮间距
}
},
methods: {
chooseType(index, item) {
if (this.selectIndex === index) return
this.selectIndex = index
this.$emit('chooseType', { item, index })
}
}
}
</script>
<style scoped lang='scss'>
.button-group {
display: flex;
align-items: center;
/* 移除容器背景,因为现在由外部 filter-container 提供 */
background: transparent;
height: toRpx(64); /* 略微减小高度 */
.button-item {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
/* width由行内样式控制 */
/* margin-right由行内样式控制 */
padding: 0;
font-size: toRpx(26);
color: #555;
border-radius: toRpx(32); /* 胶囊形状 */
transition: all 0.2s ease;
cursor: pointer;
background: #ffffff; /* 默认白底 */
border: 1px solid #ebedf0; /* 默认浅色边框 */
position: relative;
box-sizing: border-box;
overflow: hidden; /* 确保动画不溢出 */
.label {
position: relative;
z-index: 2; /* 文字在最上层 */
}
/* 激活状态的背景层 */
.active-bg {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
background: #eef4ff;
z-index: 1;
animation: pulse-fade 0.3s cubic-bezier(0.25, 0.8, 0.5, 1) forwards;
}
&.active {
color: #3773f8;
font-weight: 500;
border-color: #3773f8;
box-shadow: 0 toRpx(2) toRpx(8) rgba(55, 115, 248, 0.15);
}
}
}
@keyframes pulse-fade {
0% {
opacity: 0;
transform: scale(0.8);
}
100% {
opacity: 1;
transform: scale(1);
}
}
</style>