StarRating.vue 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434
  1. <template>
  2. <view
  3. class="star-rating"
  4. @touchstart="onTouchStart"
  5. @touchmove="onTouchMove"
  6. @touchend="onTouchEnd"
  7. >
  8. <!-- 星星容器 -->
  9. <view
  10. v-for="(item, index) in count"
  11. :key="index"
  12. class="star-container"
  13. @click="handleClick(index)"
  14. >
  15. <!-- 灰色背景星星 -->
  16. <view class="star-background">
  17. <u-icon name="star-fill" :color="inactiveColor" :size="size"></u-icon>
  18. </view>
  19. <!-- 黄色前景星星,使用宽度百分比控制 -->
  20. <view
  21. class="star-foreground"
  22. :style="{ width: getStarWidth(index), color: activeColor }"
  23. >
  24. <u-icon name="star-fill" :color="activeColor" :size="size"></u-icon>
  25. </view>
  26. </view>
  27. <!-- 可选:显示评分值 -->
  28. <text v-if="showScore" class="score-text">{{ displayValue }}</text>
  29. </view>
  30. </template>
  31. <script>
  32. export default {
  33. name: 'StarRating',
  34. props: {
  35. // 当前评分值(支持小数)
  36. value: {
  37. type: Number,
  38. default: 0
  39. },
  40. // 星星总数
  41. count: {
  42. type: Number,
  43. default: 5
  44. },
  45. // 是否禁用点击
  46. disabled: {
  47. type: Boolean,
  48. default: false
  49. },
  50. // 是否显示分数
  51. showScore: {
  52. type: Boolean,
  53. default: false
  54. },
  55. // 星星大小
  56. size: {
  57. type: [Number, String],
  58. default: 28
  59. },
  60. // 选中颜色
  61. activeColor: {
  62. type: String,
  63. default: '#ffd21e'
  64. },
  65. // 未选中颜色
  66. inactiveColor: {
  67. type: String,
  68. default: '#e0e0e0'
  69. },
  70. // 是否允许半星
  71. allowHalf: {
  72. type: Boolean,
  73. default: true
  74. },
  75. // 点击时是否启用动画
  76. animation: {
  77. type: Boolean,
  78. default: true
  79. },
  80. // 是否启用滑动评分
  81. swipeable: {
  82. type: Boolean,
  83. default: true
  84. }
  85. },
  86. data() {
  87. return {
  88. currentValue: this.value,
  89. // 滑动相关
  90. startX: 0,
  91. starWidth: 0,
  92. isSwiping: false,
  93. touchId: null
  94. }
  95. },
  96. computed: {
  97. // 格式化显示的值
  98. displayValue() {
  99. return this.currentValue.toFixed(1);
  100. }
  101. },
  102. watch: {
  103. value(newVal) {
  104. this.currentValue = newVal;
  105. }
  106. },
  107. mounted() {
  108. // 获取星星宽度,用于滑动计算
  109. this.$nextTick(() => {
  110. this.calculateStarWidth();
  111. });
  112. },
  113. methods: {
  114. // 计算每个星星的填充百分比
  115. getStarWidth(index) {
  116. const starIndex = index + 1;
  117. const value = this.currentValue;
  118. // 如果当前星星完全填充
  119. if (value >= starIndex) {
  120. return '100%';
  121. }
  122. // 如果当前星星部分填充
  123. if (value > starIndex - 1) {
  124. if (this.allowHalf) {
  125. // 计算小数部分
  126. const decimalPart = value - (starIndex - 1);
  127. return (decimalPart * 100) + '%';
  128. } else {
  129. // 如果不允许半星,则要么全满要么全空
  130. return '0%';
  131. }
  132. }
  133. // 当前星星为空
  134. return '0%';
  135. },
  136. // 计算星星宽度
  137. calculateStarWidth() {
  138. const query = uni.createSelectorQuery().in(this);
  139. query.select('.star-container').boundingClientRect(data => {
  140. if (data) {
  141. this.starWidth = data.width;
  142. }
  143. }).exec();
  144. },
  145. // 触摸开始
  146. onTouchStart(e) {
  147. if (this.disabled || !this.swipeable) return;
  148. this.isSwiping = true;
  149. this.startX = e.touches[0].clientX;
  150. this.touchId = Date.now();
  151. },
  152. // 触摸移动
  153. onTouchMove(e) {
  154. if (this.disabled || !this.swipeable || !this.isSwiping) return;
  155. const currentX = e.touches[0].clientX;
  156. const deltaX = currentX - this.startX;
  157. // 如果没有星星宽度数据,则返回
  158. if (!this.starWidth) return;
  159. // 计算移动的距离对应的星星数量
  160. let starDelta = deltaX / this.starWidth;
  161. // 根据移动方向计算新评分
  162. let newValue = this.currentValue;
  163. if (starDelta > 0) {
  164. // 向右滑动,增加评分
  165. newValue = Math.min(this.count, this.currentValue + starDelta);
  166. } else if (starDelta < 0) {
  167. // 向左滑动,减少评分
  168. newValue = Math.max(0, this.currentValue + starDelta);
  169. }
  170. // 如果不允许半星,则取整
  171. if (!this.allowHalf) {
  172. newValue = Math.round(newValue);
  173. } else {
  174. // 允许半星时,保留一位小数
  175. newValue = Math.round(newValue * 2) / 2;
  176. }
  177. // 限制范围
  178. newValue = Math.max(0, Math.min(newValue, this.count));
  179. // 更新值
  180. if (newValue !== this.currentValue) {
  181. this.currentValue = newValue;
  182. // 节流触发事件
  183. if (this.touchId) {
  184. clearTimeout(this.touchId);
  185. this.touchId = setTimeout(() => {
  186. this.$emit('input', this.currentValue);
  187. this.$emit('change', this.currentValue);
  188. }, 50);
  189. }
  190. }
  191. },
  192. // 触摸结束
  193. onTouchEnd(e) {
  194. if (this.disabled || !this.swipeable) return;
  195. this.isSwiping = false;
  196. // 触发最终事件
  197. this.$emit('input', this.currentValue);
  198. this.$emit('change', this.currentValue);
  199. // 添加动画效果
  200. if (this.animation) {
  201. this.animateAllStars();
  202. }
  203. if (this.touchId) {
  204. clearTimeout(this.touchId);
  205. this.touchId = null;
  206. }
  207. },
  208. // 处理星星点击
  209. handleClick(index) {
  210. if (this.disabled) return;
  211. const starIndex = index + 1;
  212. let newValue;
  213. if (this.allowHalf) {
  214. // 如果允许半星,点击时切换 0.5 的增量
  215. const currentStarValue = this.currentValue;
  216. if (currentStarValue >= starIndex) {
  217. // 如果点击已选中的星星,则减0.5
  218. newValue = starIndex - 0.5;
  219. } else if (currentStarValue >= starIndex - 0.5) {
  220. // 如果点击半星,则设为全星
  221. newValue = starIndex;
  222. } else {
  223. // 如果点击未选中的星星,则设为半星
  224. newValue = starIndex - 0.5;
  225. }
  226. } else {
  227. // 如果不允许半星,则直接设为整星
  228. newValue = starIndex;
  229. }
  230. // 限制在0到count之间
  231. newValue = Math.max(0, Math.min(newValue, this.count));
  232. // 更新值
  233. this.currentValue = newValue;
  234. // 触发动画
  235. if (this.animation) {
  236. this.animateStar(index);
  237. }
  238. // 触发事件
  239. this.$emit('input', newValue);
  240. this.$emit('change', newValue);
  241. },
  242. // 星星点击动画
  243. animateStar(index) {
  244. const stars = this.$el.querySelectorAll('.star-container');
  245. if (stars && stars[index]) {
  246. stars[index].classList.add('star-animate');
  247. setTimeout(() => {
  248. stars[index].classList.remove('star-animate');
  249. }, 300);
  250. }
  251. },
  252. // 所有星星动画
  253. animateAllStars() {
  254. const stars = this.$el.querySelectorAll('.star-container');
  255. if (stars) {
  256. stars.forEach((star, index) => {
  257. if (this.currentValue > index) {
  258. setTimeout(() => {
  259. star.classList.add('star-animate');
  260. setTimeout(() => {
  261. star.classList.remove('star-animate');
  262. }, 300);
  263. }, index * 50);
  264. }
  265. });
  266. }
  267. },
  268. // 设置评分(可以从外部调用)
  269. setRating(value) {
  270. if (this.disabled) return;
  271. let newValue = parseFloat(value);
  272. if (isNaN(newValue)) return;
  273. // 限制范围
  274. newValue = Math.max(0, Math.min(newValue, this.count));
  275. // 如果不允许半星,则四舍五入到整数
  276. if (!this.allowHalf) {
  277. newValue = Math.round(newValue);
  278. } else {
  279. // 允许半星时,保留一位小数
  280. newValue = Math.round(newValue * 2) / 2;
  281. }
  282. this.currentValue = newValue;
  283. this.$emit('input', newValue);
  284. this.$emit('change', newValue);
  285. // 触发动画
  286. if (this.animation) {
  287. this.animateAllStars();
  288. }
  289. },
  290. // 重置评分
  291. reset() {
  292. this.currentValue = 0;
  293. this.$emit('input', 0);
  294. this.$emit('change', 0);
  295. }
  296. }
  297. }
  298. </script>
  299. <style lang="scss" scoped>
  300. .star-rating {
  301. display: inline-flex;
  302. align-items: center;
  303. font-size: 0;
  304. touch-action: pan-x;
  305. user-select: none;
  306. .star-container {
  307. position: relative;
  308. display: inline-block;
  309. width: 56rpx;
  310. height: 56rpx;
  311. // margin-right: 8rpx;
  312. transition: transform 0.3s ease;
  313. &:last-child {
  314. margin-right: 0;
  315. }
  316. &.star-animate {
  317. animation: starPulse 0.3s ease;
  318. }
  319. .star-background {
  320. position: absolute;
  321. top: 0;
  322. left: 0;
  323. z-index: 1;
  324. }
  325. .star-foreground {
  326. position: absolute;
  327. top: 0;
  328. left: 0;
  329. z-index: 2;
  330. overflow: hidden;
  331. height: 56rpx;
  332. }
  333. }
  334. .score-text {
  335. margin-left: 16rpx;
  336. font-size: 28rpx;
  337. color: #f0ad4e;
  338. font-weight: 500;
  339. }
  340. }
  341. // 动画效果
  342. @keyframes starPulse {
  343. 0% {
  344. transform: scale(1);
  345. }
  346. 50% {
  347. transform: scale(1.2);
  348. }
  349. 100% {
  350. transform: scale(1);
  351. }
  352. }
  353. // 设置不同大小的星星
  354. .star-rating.size-small {
  355. .star-container {
  356. width: 32rpx;
  357. height: 32rpx;
  358. margin-right: 4rpx;
  359. .star-foreground {
  360. height: 32rpx;
  361. }
  362. }
  363. }
  364. .star-rating.size-large {
  365. .star-container {
  366. width: 80rpx;
  367. height: 80rpx;
  368. margin-right: 12rpx;
  369. .star-foreground {
  370. height: 80rpx;
  371. }
  372. }
  373. }
  374. // 禁用状态
  375. .star-rating.disabled {
  376. opacity: 0.6;
  377. .star-container {
  378. cursor: not-allowed;
  379. }
  380. }
  381. </style>