tab-block.vue
2.19 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
<template>
<div class="tab-block">
<div v-for="(tab, index) in tabList" :key="index" :ref="'tab-item' + index" class="tab-item" :class="{active: index == activeIndex}" @click="changeType(index)">
<div class="name">{{tab.name}}</div>
<div v-if="index == statusBlockIndex" ref="statusBlock" class="tab-status-block" :style="statusBlockStyle"></div>
</div>
</div>
</template>
<script>
import {findIndex} from 'lodash';
export default {
props: {
activeType: {
type: Number,
default: 0
}
},
data() {
let tabList = [
{name: '最新', type: 2},
{name: '热门', type: 1},
];
let index = Math.max(findIndex(tabList, {type: this.activeType}), 0);
return {
tabList,
statusBlockIndex: index,
activeIndex: index,
statusBlockStyle: {}
};
},
watch: {
activeType(val) {
let index = findIndex(this.tabList, {type: val});
if (index > -1) {
this.activeIndex = index;
}
},
activeIndex() {
this.changeStatusBlockStyle();
}
},
methods: {
changeType(index) {
this.activeIndex = index;
this.$emit('on-change-tab', {...this.tabList[index]});
},
changeStatusBlockStyle() {
const baseDom = this.$refs['tab-item' + this.statusBlockIndex];
if (baseDom) {
const baseLeft = baseDom[0].offsetLeft;
const {offsetLeft, offsetWidth} = this.$refs['tab-item' + this.activeIndex][0] || {};
this.statusBlockStyle = {
transform: `translate3d(${offsetLeft - baseLeft}px, 0, 0)`,
width: offsetWidth + 'px'
};
}
}
}
};
</script>
<style scoped lang="scss">
.tab-block {
height: 100px;
background-color: #fff;
display: flex;
justify-content: center;
font-size: 32px;
.tab-item {
margin: 0 38px;
padding-top: 20px;
color: #b0b0b0;
position: relative;
&.active {
color: #222;
font-weight: 500;
}
.name {
margin-bottom: 4px;
line-height: 1.4;
}
}
.tab-status-block {
width: 100%;
height: 10px;
background: #d0021b;
box-shadow: 0 4px 8px 0 rgba(210, 0, 13, 0.34);
position: absolute;
transition: all 300ms;
}
}
</style>