forked from dyq086/wepy-mall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rate.wpy
80 lines (71 loc) · 1.78 KB
/
rate.wpy
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
<template>
<block wx:for="{{stars}}" wx:key="item.id">
<image wx:if="{{readonly}}" class="star-image-read" style="left: {{item}}rpx" src="{{key > item ?(key-item == 0.5?halfSrc:selectedSrc) : normalSrc}}">
</image>
<image wx:else class="star-image" style="left: {{item*10}}rpx" src="{{key > item ?(key-item == 0.5?halfSrc:selectedSrc) : normalSrc}}">
<view class="item" style="left:0rpx" data-key="{{item+0.5}}" @tap="selectLeft"></view>
<view class="item" style="left:25rpx" data-key="{{item+1}}" @tap="selectRight"></view>
</image>
</block>
</template>
<script>
import wepy from 'wepy';
export default class Rate extends wepy.component {
props = {
readonly: {
default: false
},
key: {
default: 0
},
}
data = {
stars: [0, 1, 2, 3, 4],
normalSrc: '../images/normal.png',
selectedSrc: '../images/selected.png',
halfSrc: '../images/half.png',
}
events = {
}
methods = {
//点击右边,半颗星
selectLeft: function(e) {
var key = e.currentTarget.dataset.key;
if (this.data.key == 0.5 && e.currentTarget.dataset.key == 0.5) {
//只有一颗星的时候,再次点击,变为0颗
this.key = 0;
}
this.key = key;
this.$emit("change", this.key)
},
//点击左边,整颗星
selectRight: function(e) {
var key = e.currentTarget.dataset.key;
this.key = key;
this.$emit("change", this.key)
}
}
onLoad() {
}
}
</script>
<style lang="less">
.star-image {
position: relative;
width: 50rpx;
height: 50rpx;
src: "../images/normal.png";
.item {
position: absolute;
width: 25rpx;
top: 0;
height: 50rpx;
}
}
.star-image-read {
position: relative;
width: 30rpx;
height: 30rpx;
src: "../images/normal.png";
}
</style>