uni-rate.vue 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <template>
  2. <view class="uni-rate" :class="['uni-rate-'+type]">
  3. <view :key="index" :style="{ marginLeft: margin + 'px' }" @click="_onClick(index)" class="uni-rate__icon" v-for="(star, index) in stars">
  4. <image class="uni-rate-icon" :src="valueSync>index?checkIcon:icon"></image>
  5. </view>
  6. </view>
  7. </template>
  8. <script>
  9. import uniIcons from "../uni-icons/uni-icons.vue";
  10. export default {
  11. name: "UniRate",
  12. components: {
  13. uniIcons
  14. },
  15. props: {
  16. color: {
  17. // 星星的颜色
  18. type: String,
  19. default: "#ececec"
  20. },
  21. activeColor: {
  22. // 星星选中状态颜色
  23. type: String,
  24. default: "#ffca3e"
  25. },
  26. size: {
  27. // 星星的大小
  28. type: [Number, String],
  29. default: 24
  30. },
  31. value: {
  32. // 当前评分
  33. type: [Number, String],
  34. default: 0
  35. },
  36. max: {
  37. // 最大评分
  38. type: [Number, String],
  39. default: 5
  40. },
  41. margin: {
  42. // 星星的间距
  43. type: [Number, String],
  44. default: 5
  45. },
  46. disabled: {
  47. // 是否可点击
  48. type: [Boolean, String],
  49. default: false
  50. },
  51. type:{
  52. type:String,
  53. default:'default'
  54. },
  55. },
  56. data() {
  57. return {
  58. valueSync: "",
  59. icon: require('./images/rate.png'),
  60. checkIcon:require('./images/rate-active.png')
  61. };
  62. },
  63. computed: {
  64. stars() {
  65. const value = this.valueSync ? this.valueSync : 0;
  66. const starList = [];
  67. const floorValue = Math.floor(value);
  68. const ceilValue = Math.ceil(value);
  69. // console.log("ceilValue: " + ceilValue);
  70. // console.log("floorValue: " + floorValue);
  71. for (let i = 0; i < this.max; i++) {
  72. if (floorValue > i) {
  73. starList.push({
  74. activeWitch: "100%"
  75. });
  76. } else if (ceilValue - 1 === i) {
  77. starList.push({
  78. activeWitch: (value - floorValue) * 100 + "%"
  79. });
  80. } else {
  81. starList.push({
  82. activeWitch: "0"
  83. });
  84. }
  85. }
  86. //console.log("starList[4]: " + starList[4].activeWitch);
  87. return starList;
  88. }
  89. },
  90. created() {
  91. this.valueSync = Number(this.value);
  92. },
  93. methods: {
  94. _onClick(index) {
  95. if (this.disabled) {
  96. return;
  97. }
  98. this.valueSync = index + 1;
  99. this.$emit("input", this.valueSync);
  100. this.$emit("change", {
  101. value: this.valueSync
  102. });
  103. }
  104. }
  105. };
  106. </script>
  107. <style lang="scss" scoped>
  108. .uni-rate {
  109. /* #ifndef APP-NVUE */
  110. display: flex;
  111. /* #endif */
  112. line-height: 0;
  113. font-size: 0;
  114. flex-direction: row;
  115. }
  116. .uni-rate-icon{
  117. width: 22upx;
  118. height: 22upx;
  119. }
  120. .uni-rate-comment .uni-rate-icon{
  121. width: 30upx;
  122. height: 30upx;
  123. }
  124. .uni-rate-icon:first-child{
  125. margin-left: 0 !important;
  126. }
  127. .uni-rate__icon {
  128. position: relative;
  129. line-height: 0;
  130. font-size: 0;
  131. display: inline-block;
  132. }
  133. .uni-rate__icon-on {
  134. overflow: hidden;
  135. position: absolute;
  136. top: 0;
  137. left: 0;
  138. line-height: 1;
  139. text-align: left;
  140. }
  141. </style>