uniapp 绘制五星评分 精确到半星

uniapp 绘制五星评分 精确到半星

  1. 对比上一篇博客 这个很简单
  2. 上组件
javascript 复制代码
<template>
	<div :class="[imgType=='other'?'image-box':'image-box1']">
		<img :class="[imgType=='other'?'image':'image1']" v-for="(el,i) in value" :key="i" :src="el" alt="">
	</div>
</template>


<script>
	export default {
		props: {
			type: {
				type: Number,
				default: 1 // 总分5, 总分 100
			},
			score: {
				type: Number,
				default: 0 // 得分50,得分 100
			},
			imgType: {
				type: String,
				default: 'other' // wonder:奇观, other:其他
			}
		},
		data() {
			return {
				wonder: [
					'https://eshopfile.zhiyousx.com/173284619428810213.png',
					'https://eshopfile.zhiyousx.com/173284619428405733.png',
					'https://eshopfile.zhiyousx.com/173284619428491512.png'
				], //奇观
				other: [
					"https://eshopfile.zhiyousx.com/173284605868930990.png",
					"https://eshopfile.zhiyousx.com/173284605869004457.png",
					"https://eshopfile.zhiyousx.com/173284605868490597.png",
				], //其他
			};
		},
		computed: {
			// 转换得分
			value() {
				// 得分为0
				if (!this.score) {
					let newArr = []
					for (var i = 0; i < 5; i++) {
						newArr.push(this[this.imgType][2])
					}
					return newArr
				}
				// 得分不为0
				let value = this.type == 2 ? (this.score / 20).toFixed(1) : this.score;
				let arr = []
				// 空星 向后添加
				let empty = 5 - Math.ceil(value);
				for (let i = 1; i < 6; i++) {
					if (value >= i) {
						arr.push(this[this.imgType][0]); // 全星
					} else {
						if (value < i && Math.ceil(value) == i) {
							let num = this.getNumber(this.score) - 1
							console.log(num)
							arr.push(this[this.imgType][num]); // 半星

						} else {
							arr.push(this[this.imgType][2]); // 空星
						}
					}
				}
				return arr
			},
		},
		methods: {
			getNumber(input) {
				input = this.type == 2 ? (input / 20).toFixed(1) : input;
				const decimalPart = (input * 10 - Math.floor(input) * 10) / 10; // 获取小数部分
				if (decimalPart >= 0.1 && decimalPart <= 0.2) {
					return 3;
				} else if (decimalPart >= 0.3 && decimalPart <= 0.7) {
					return 2;
				} else if (decimalPart >= 0.8 && decimalPart <= 0.9) {
					return 1;
				}
			}
		}
	};
</script>
<style lang="scss" scoped>
	.image {
		width: 24rpx;
		height: 24rpx;
		margin-right: 2rpx;
	}
	.image1 {
		width: 28rpx;
		height: 28rpx;
		// margin-right: 2rpx;
	}
	.image-box {
		height: 30rpx;
		width: 130rpx;
		text-align: center;
	}
	.image-box1 {
		height: 30rpx;
		width: 140rpx;
		text-align: center;
	}
</style>
  1. 组件使用
javascript 复制代码
<template>
	<view class="external-box">
       <starRate :score="100" />
	</view>
</template>

<script>
	import starRate from "../components/starRateImg.vue";
	export default {
		components: {
			starRate,
		},
	};
</script>

<style lang="scss" scoped>

</style>
  1. 该组件 判断规则 0-0.2 为 0星 0.3-0.7 半星 0.8-1 1星

对比上一个博客 该组件更简单 更直观 无适配问题 但精确度不够 可以增加图片 提高精度。

`

相关推荐
前端Hardy2 分钟前
用 uni-app x 重构我们的 App:一套代码跑通 iOS、Android、鸿蒙!人力成本直降 60%
前端·ios·uni-app
江不清丶2 分钟前
Text-to-SQL实战:从自然语言到数据库查询的智能数据分析Agent设计
数据库·sql·ai·数据分析
初次攀爬者10 分钟前
Redis与数据库的数据一致性方案解析
数据库·redis·分布式
橘颂TA21 分钟前
【MySQL】内置函数
数据库·mysql
八月瓜科技27 分钟前
擎策·知海全球专利数据库 凭差异化优势 筑科技创新检索壁垒
大数据·数据库·人工智能·科技·深度学习·机器人
搜佛说35 分钟前
sfsEdgeStore轻量级边缘计算数据存储适配平台
数据库·人工智能·物联网·边缘计算·iot
橘颂TA1 小时前
【MySQL】使用C/C++来连接 MySQL
数据库·mysql
happyboy19862111 小时前
2026大专财富管理可以转数据分析吗?
数据库·数据挖掘·数据分析
杰克尼1 小时前
苍穹外卖--day11
java·数据库·spring boot·mybatis·notepad++
LaughingZhu1 小时前
Product Hunt 每日热榜 | 2026-03-12
大数据·数据库·人工智能·经验分享·搜索引擎