tn-rate.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. <template>
  2. <view
  3. :id="elId"
  4. class="tn-rate-class tn-rate"
  5. @touchmove.stop.prevent="touchMove"
  6. >
  7. <view class="tn-rate__wrap" :class="[elClass]" v-for="(item, index) in count" :key="index">
  8. <view
  9. class="tn-rate__wrap__icon"
  10. :class="[`tn-icon-${(allowHalf && halfIcon ? activeIndex > index + 1 : activeIndex > index) ? elActionIcon : elInactionIcon}`]"
  11. :style="[iconStyle(index)]"
  12. @tap="click(index + 1, $event)"
  13. >
  14. <!-- 半图标 -->
  15. <view
  16. v-if="showHalfIcon(index)"
  17. class="tn-rate__wrap__icon--half"
  18. :class="[`tn-icon-${elActionIcon}`]"
  19. :style="[halfIconStyle]"
  20. ></view>
  21. </view>
  22. </view>
  23. </view>
  24. </template>
  25. <script>
  26. export default {
  27. name: 'tn-rate',
  28. props: {
  29. // 选中星星的数量
  30. value: {
  31. type: Number,
  32. default: 0
  33. },
  34. // 显示的星星数
  35. count: {
  36. type: Number,
  37. default: 5
  38. },
  39. // 最小能选择的星星数
  40. minCount: {
  41. type: Number,
  42. default: 0
  43. },
  44. // 禁用状态
  45. disabled: {
  46. type: Boolean,
  47. default: false
  48. },
  49. // 是否可以选择半星
  50. allowHalf: {
  51. type: Boolean,
  52. default: false
  53. },
  54. // 星星大小
  55. size: {
  56. type: Number,
  57. default: 32
  58. },
  59. // 被选中的图标
  60. activeIcon: {
  61. type: String,
  62. default: 'star-fill'
  63. },
  64. // 未被选中的图标
  65. inactiveIcon: {
  66. type: String,
  67. default: 'star'
  68. },
  69. // 被选中的颜色
  70. activeColor: {
  71. type: String,
  72. default: '#01BEFF'
  73. },
  74. // 默认颜色
  75. inactiveColor: {
  76. type: String,
  77. default: '#AAAAAA'
  78. },
  79. // 星星之间的距离
  80. gutter: {
  81. type: Number,
  82. default: 10
  83. },
  84. // 自定义颜色
  85. colors: {
  86. type: Array,
  87. default() {
  88. return []
  89. }
  90. },
  91. // 自定义图标
  92. icons: {
  93. type: Array,
  94. default() {
  95. return []
  96. }
  97. }
  98. },
  99. computed: {
  100. // 图标显示的比例
  101. showHalfIcon(index) {
  102. return index => {
  103. return this.allowHalf && Math.ceil(this.activeIndex) === index + 1 && this.halfIcon
  104. }
  105. },
  106. // 被激活的图标
  107. elActionIcon() {
  108. const len = this.icons.length
  109. // icons参数传递了3个图标,当选中2个时,用第一个图标,4个时,用第二个图标
  110. // 5个时,用第三个图标作为激活的图标
  111. if (len && len <= this.count) {
  112. const step = Math.round(Math.ceil(this.activeIndex) / Math.round(this.count / len))
  113. if (step < 1) return this.icons[0]
  114. if (step > len) return this.icons[len - 1]
  115. return this.icons[step - 1]
  116. }
  117. return this.activeIcon
  118. },
  119. // 未被激活的图标
  120. elInactionIcon() {
  121. const len = this.icons.length
  122. // icons参数传递了3个图标,当选中2个时,用第一个图标,4个时,用第二个图标
  123. // 5个时,用第三个图标作为激活的图标
  124. if (len && len <= this.count) {
  125. const step = Math.round(Math.ceil(this.activeIndex) / Math.round(this.count / len))
  126. if (step < 1) return this.icons[0]
  127. if (step > len) return this.icons[len - 1]
  128. return this.icons[step - 1]
  129. }
  130. return this.inactiveIcon
  131. },
  132. // 被激活的颜色
  133. elActionColor() {
  134. const len = this.colors.length
  135. // 如果有设置colors参数(此参数用于将图标分段,比如一共5颗星,colors传3个颜色值,那么根据一定的规则,2颗星可能为第一个颜色
  136. // 4颗星为第二个颜色值,5颗星为第三个颜色值)
  137. if (len && len <= this.count) {
  138. const step = Math.round(Math.ceil(this.activeIndex) / Math.round(this.count / len))
  139. if (step < 1) return this.colors[0]
  140. if (step > len) return this.colors[len - 1]
  141. return this.colors[step - 1]
  142. }
  143. return this.activeColor
  144. },
  145. // 图标的样式
  146. iconStyle() {
  147. return index => {
  148. let style = {}
  149. style.fontSize = this.$t.string.getLengthUnitValue(this.size)
  150. style.padding = `0 ${this.$t.string.getLengthUnitValue(this.gutter / 2)}`
  151. // 当前图标的颜色
  152. if (this.allowHalf && this.halfIcon) {
  153. style.color = this.activeIndex > index + 1 ? this.elActionColor : this.inactiveColor
  154. } else {
  155. style.color = this.activeIndex > index ? this.elActionColor : this.inactiveColor
  156. }
  157. return style
  158. }
  159. },
  160. // 半图标样式
  161. halfIconStyle() {
  162. let style = {}
  163. style.fontSize = this.$t.string.getLengthUnitValue(this.size)
  164. style.padding = `0 ${this.$t.string.getLengthUnitValue(this.gutter / 2)}`
  165. style.color = this.elActionColor
  166. return style
  167. }
  168. },
  169. data() {
  170. return {
  171. // 保证控件的唯一性
  172. elId: this.$t.uuid(),
  173. elClass: this.$t.uuid(),
  174. // 评分盒子左边到屏幕左边的距离,用于滑动选择时计算距离
  175. starBoxLeft: 0,
  176. // 当前激活的星星的序号
  177. activeIndex: this.value,
  178. // 每个星星的宽度
  179. starWidth: 0,
  180. // 每个星星最右边到盒子组件最左边的距离
  181. starWidthArr: [],
  182. // 标记是否为半图标
  183. halfIcon: false,
  184. }
  185. },
  186. watch: {
  187. value(val) {
  188. this.activeIndex = val
  189. if (this.allowHalf && (val % 1 === 0.5)) {
  190. this.halfIcon = true
  191. } else {
  192. this.halfIcon = false
  193. }
  194. },
  195. size() {
  196. // 当尺寸修改的时候重新获取布局尺寸信息
  197. this.$nextTick(() => {
  198. this.getElRectById()
  199. this.getElRectByClass()
  200. })
  201. }
  202. },
  203. mounted() {
  204. this.getElRectById()
  205. this.getElRectByClass()
  206. },
  207. methods: {
  208. // 获取评分组件盒子的布局信息
  209. getElRectById() {
  210. this._tGetRect('#'+this.elId).then(res => {
  211. this.starBoxLeft = res.left
  212. })
  213. },
  214. // 获取单个星星的尺寸
  215. getElRectByClass() {
  216. this._tGetRect('.'+this.elClass).then(res => {
  217. this.starWidth = res.width
  218. // 把每个星星最右边到盒子最左边的距离
  219. for (let i = 0; i < this.count; i++) {
  220. this.starWidthArr[i] = (i + 1) * this.starWidth
  221. }
  222. })
  223. },
  224. // 手指滑动
  225. touchMove(e) {
  226. if (this.disabled) return
  227. if (!e.changedTouches[0]) return
  228. const movePageX = e.changedTouches[0].pageX
  229. // 滑动点相对于评分盒子左边的距离
  230. const distance = movePageX - this.starBoxLeft
  231. // 如果滑动到了评分盒子的左边界,设置为0星
  232. if (distance <= 0) {
  233. this.activeIndex = 0
  234. }
  235. // 计算滑动的距离相当于点击多少颗星星
  236. let index = Math.ceil(distance / this.starWidth)
  237. if (this.allowHalf) {
  238. const iconHalfWidth = this.starWidthArr[index - 1] - (this.starWidth / 2)
  239. if (distance < iconHalfWidth) {
  240. this.halfIcon = true
  241. index -= 0.5
  242. } else {
  243. this.halfIcon = false
  244. }
  245. }
  246. this.activeIndex = index > this.count ? this.count : index
  247. if (this.activeIndex < this.minCount) this.activeIndex = this.minCount
  248. this.emitEvent()
  249. },
  250. // 通过点击直接选中
  251. click(index, e) {
  252. if (this.disabled) return
  253. // 半星选择
  254. if (this.allowHalf) {
  255. if (!e.changedTouches[0]) return
  256. const movePageX = e.changedTouches[0].pageX
  257. // 点击点相对于当前图标左边的距离
  258. const distance = movePageX - this.starBoxLeft
  259. const iconHalfWidth = this.starWidthArr[index - 1] - (this.starWidth / 2)
  260. if (distance < iconHalfWidth) {
  261. this.halfIcon = true
  262. } else {
  263. this.halfIcon = false
  264. }
  265. }
  266. // 对第一个星星特殊处理,只有一个的时候,点击可以取消,否则无法作0星评价
  267. if (index == 1) {
  268. if (this.allowHalf && this.allowHalf) {
  269. if ((this.activeIndex === 0.5 && this.halfIcon) ||
  270. (this.activeIndex === 1 && !this.halfIcon)) {
  271. this.activeIndex = 0
  272. } else {
  273. this.activeIndex = this.halfIcon ? 0.5 : 1
  274. }
  275. } else {
  276. if (this.activeIndex == 1) {
  277. this.activeIndex = 0
  278. } else {
  279. this.activeIndex = 1
  280. }
  281. }
  282. } else {
  283. this.activeIndex = (this.allowHalf && this.halfIcon) ? index - 0.5 : index
  284. }
  285. if (this.activeIndex < this.minCount) this.activeIndex = this.minCount
  286. this.emitEvent()
  287. },
  288. // 发送事件
  289. emitEvent() {
  290. this.$emit('change', this.activeIndex)
  291. // 修改v-model的值
  292. this.$emit('input', this.activeIndex)
  293. }
  294. }
  295. }
  296. </script>
  297. <style lang="scss" scoped>
  298. .tn-rate {
  299. display: inline-flex;
  300. align-items: center;
  301. margin: 0;
  302. padding: 0;
  303. &__wrap {
  304. &__icon {
  305. position: relative;
  306. box-sizing: border-box;
  307. &--half {
  308. position: absolute;
  309. top: 0;
  310. left: 0;
  311. display: inline-block;
  312. overflow: hidden;
  313. width: 50%;
  314. }
  315. }
  316. }
  317. }
  318. </style>