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星

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

`

相关推荐
2601_962218615 小时前
万象生鲜系统业财一体化底层打通技术自动生成经营账单
大数据·数据库·人工智能·python·算法
李高钢6 小时前
Python FastAPI 框架入门:从零搭建你的第一个高性能 API 服务
数据库·python·fastapi
yuzhiboyouye8 小时前
那xml对应的sql语法,列举一下
xml·数据库·sql
. . . . .8 小时前
乐观锁 vs 悲观锁
数据库·sql
Leo.yuan8 小时前
2026年多数据库实时同步的四种架构方案及工具推荐
数据库·架构
hasty9 小时前
Origin 校验不是身份认证:MySQL MCP Server 漏洞给 AI 平台的警告
数据库·mysql·安全
EatFan9 小时前
Java接入支付宝 JSAPI 支付保姆教程(二):流程讲解与前后端代码讲解
前端·spring boot·后端·微信小程序·小程序·uni-app
梦想平凡10 小时前
百游棋牌源代码开发搭建教程(五):房间创建、座位分配与请求幂等实现
前端·javascript·数据库·源代码管理
凤山老林10 小时前
Spring Boot 整合 Flowable 的企业级落地指南
数据库·spring boot·后端·flowable·工作流
企业数字化笔记10 小时前
AI写的系统出现504怎么办?接口超时和数据库慢查询排查
数据库·后端