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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
| <template>
| <view :class="{ 'uni-scroll-tab': scroll === true }" class="uni-tab">
| <view
| v-for="(tab, index) in tabList"
| :key="index"
| :class="{
| 'uni-tab-active': index === value,
| 'uni-tab-scroll-item': scroll === true,
| ' uni-tab-scroll-active': index === value && scroll === true
| }"
| :style="[
| {
| color: index === value ? activeColor : defaultColor,
| backgroundColor: bgColor
| }
| ]"
| style="cursor: pointer;"
| @tap="itemClick(index, tab)"
| class="uni-tab-item"
| >
| <span
| v-if="tab.icon != undefined"
| class="iconfont mr5"
| :class="tab.icon"
| ></span>
|
| <text>{{ rangeKey == "" ? tab.TabControl : tab[rangeKey] }}</text>
| </view>
| <view
| v-if="!scroll"
| :style="[
| { right: barRight + '%', left: barLeft + '%', borderColor: activeColor }
| ]"
| class="uni-tab-bar"
| :class="back ? 'uni-tab-bar-backward' : 'uni-tab-bar-forward'"
| ></view>
| </view>
| </template>
| <script>
| export default {
| name: "uni-tab",
| data() {
| return {
| average: 0,
| back: false
| };
| },
| props: {
| value: {
| type: Number, //当前选中下标
| default() {
| return 0;
| }
| },
| tabList: {
| type: Array,
| default() {
| return [];
| }
| },
| bgColor: {
| //背景颜色
| type: String,
| default() {
| return "#FFFFFF";
| }
| },
| defaultColor: {
| //默认未选中文字颜色
| type: String,
| default() {
| return "#636363";
| }
| },
| activeColor: {
| //选中时文字颜色 线条颜色
| type: String,
| default() {
| return "#000000";
| }
| },
| rangeKey: {
| // 当tabList为对象时 显示指定下标值
| type: String,
| default() {
| return "";
| }
| },
| scroll: {
| //横向滑动
| type: Boolean,
| default() {
| return false;
| }
| }
| },
| computed: {
| barLeft() {
| return this.value * this.average;
| },
| barRight() {
| let index = this.tabList.length - this.value - 1;
| return index * this.average;
| }
| },
| created() {
| this.average = 100 / this.tabList.length;
| },
| methods: {
| itemClick(index, tab) {
| if (this.value == index) return false;
| if (this.value > index) {
| this.back = true;
| } else {
| this.back = false;
| }
| // this.value = index;
| this.$emit("update:value", index);
| this.$emit("change", {
| tab: tab
| });
| }
| }
| };
| </script>
| <style lang="scss" scoped>
| @import "../../static/iconfont.css";
|
| .uni-tab {
| position: relative;
| display: flex;
| font-size: 14px;
| height: 44px;
| line-height: 44px;
| .uni-tab-item {
| flex: 1;
| height: 100%;
| text-align: center;
| box-sizing: border-box;
| overflow: hidden;
| }
|
| .uni-tab-scroll-item {
| flex: none;
| padding: 0px 12px;
| }
|
| .uni-tab-active {
| color: #1e9fff;
| }
|
| .uni-tab-scroll-active {
| border-bottom: 3px solid #1e9fff;
| }
|
| .uni-tab-bar {
| display: block;
| height: 3px;
| position: absolute;
| bottom: 0;
| border-bottom: 3px solid #1e9fff;
| }
|
| .uni-tab-bar-forward {
| transition: right 0.3s cubic-bezier(0.35, 0, 0.25, 1),
| left 0.3s cubic-bezier(0.35, 0, 0.25, 1) 0.09s;
| }
|
| .uni-tab-bar-backward {
| transition: right 0.3s cubic-bezier(0.35, 0, 0.25, 1) 0.09s,
| left 0.3s cubic-bezier(0.35, 0, 0.25, 1);
| }
| }
|
| .uni-scroll-tab {
| overflow-x: scroll;
| }
| </style>
|
|