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星

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

`

相关推荐
消失在人海中2 分钟前
oracle sql 语句 优化方法
数据库·sql·oracle
Clang's Blog6 分钟前
一键搭建 WordPress + MySQL + phpMyAdmin 环境(支持 PHP 版本选择 & 自定义配置)
数据库·mysql·php·wordpr
zzc92111 分钟前
MATLAB仿真生成无线通信网络拓扑推理数据集
开发语言·网络·数据库·人工智能·python·深度学习·matlab
未来之窗软件服务32 分钟前
JAVASCRIPT 前端数据库-V1--仙盟数据库架构-—-—仙盟创梦IDE
数据库·数据库架构·仙盟创梦ide·东方仙盟数据库
LjQ204040 分钟前
网络爬虫一课一得
开发语言·数据库·python·网络爬虫
烙印60144 分钟前
MyBatis原理剖析(二)
java·数据库·mybatis
RestCloud1 小时前
如何通过ETLCloud实现跨系统数据同步?
数据库·数据仓库·mysql·etl·数据处理·数据同步·集成平台
你是狒狒吗1 小时前
TM中,return new TransactionManagerImpl(raf, fc);为什么返回是new了一个新的实例
java·开发语言·数据库
Channing Lewis2 小时前
sql server如何创建表导入excel的数据
数据库·oracle·excel
秃头摸鱼侠2 小时前
MySQL安装与配置
数据库·mysql·adb