在项目中需要用到五星评价,但是找的组件都是只能点亮半颗星 或者一颗星星,不能实现3.7,4.2这样的评分,而且此需求不能改。。。。所以就自己琢磨了一个方法。
效果如下:
思路如下:
每个小星星都是一个正方形图片,中间是透明的,然后无间隙排在一起放在一个父盒子里。父盒子高和星星一样高,宽,根据实际评分来动态实现,最宽是五分。然后给父盒子一个背景色即可
代码如下:
<view style="width: 150rpx;">
<view class="img-bg" style="width: {{3.4 / 5 * 150}}rpx" >
<view style="width: 150rpx;">
<image src="/static/image/start2.png" class="start" ></image>
<image src="/static/image/start2.png" class="start" ></image>
<image src="/static/image/start2.png" class="start" ></image>
<image src="/static/image/start2.png" class="start" ></image>
<image src="/static/image/start2.png" class="start" ></image>
</view>
</view>
</view>
//样式如下
.start {
width: 30rpx;
height: 30rpx;
vertical-align: top;
}
.img-bg {
background-color: #ec944a;
width: 150rpx;
height: 30rpx;
}