-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrate.vue
145 lines (144 loc) · 3.01 KB
/
rate.vue
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
使用组件
```
<rate :rate-value="10" :is-edit="true" @change="setV"></rate>
```
组件代码:
```
<template>
<div class="rate-box" v-if="isEdit">
<div class="item" v-for="(item,index) in list" :key="index">
<p :class="item.leftIs?'active':''" @click="itemClick(index,0)"></p>
<p :class="item.rightIs?'active':''" @click="itemClick(index,-0.5)"></p>
</div>
</div>
<div class="rate-box2" v-else>
<div>{{v}}</div>
<template v-for="(item,index) in list">
<div class="item" :key="index" v-if="item.leftIs||item.rightIs">
<p :class="item.leftIs?'active':''"></p>
<p :class="item.rightIs?'active':''"></p>
</div>
</template>
</div>
</template>
<script>
export default {
props: {
isEdit: {//是否开启编辑功能
type: Boolean,
default: false
},
total: {//总分,半星功能,0.5代表1分
type: Number,
default: 10
},
rateValue: {//初始分数
type: Number,
default: 0
}
},
data() {
return {
v: this.rateValue
};
},
computed: {
list() {
const total = this.total;
const rate = this.v;
const _ = [];
for (let i = 1; i <= total; i++) {
_.push({
leftIs: total - rate < i - 0.5,
rightIs: total - rate < i
});
}
return _;
}
},
methods: {
itemClick(index, site) {
const _ = this.total - index + site;
const result = _ == this.v ? 0 : _;
this.v = result;
this.$emit("change", result);//设置回调
}
}
};
</script>
<style lang="less" scoped>
.rate-box {
height: 18px;
display: inline-block;
&:hover {
.item > p {
background-color: #ccc;
}
}
& > div {
width: 18px;
height: 18px;
float: right;
margin-right: 8px;
transition: all 0.5s linear;
cursor: pointer;
& > p {
width: 9px;
height: 18px;
background-color: #ccc;
background-image: url("./../assets/images/xing.png");
background-repeat: no-repeat;
background-size: 200%;
float: right;
&:first-child {
background-position-x: -9px;
}
&.active {
background-color: #fadb14;
}
&:hover {
background-color: #fadb14 !important;
}
&:hover ~ p {
background-color: #fadb14 !important;
}
}
&:hover {
transform: scale(1.1);
}
&:hover ~ .item {
p {
background-color: #fadb14;
}
}
}
}
.rate-box2 {
height: 18px;
display: inline-block;
& > div {
width: 18px;
height: 18px;
float: right;
margin-right: 8px;
transition: all 0.5s linear;
cursor: pointer;
& > p {
width: 9px;
height: 18px;
background-color: #fff;
background-image: url("./../assets/images/xing.png");
background-repeat: no-repeat;
background-size: 200%;
float: right;
&:first-child {
background-position-x: -9px;
}
&.active {
background-color: #fadb14;
}
}
}
}
</style>
```