用vant的icon组件实现高德评分样式

高德评分

在这里插入图片描述

效果

在这里插入图片描述
想再像一点的话就根据rate值加个class,调下颜色就好了

vue3组件

<template>
    <!-- 评分 -->
    <div class="rate-box">
        <div class="rate-star-list">
            <div class="rate-item" v-for="a in [1, 2, 3, 4, 5]" :key="a">
                <van-icon name="star" color="#fff" size="12" class="rate-item-icon" />
                <div
                    class="grey-bg"
                    v-if="rate < a"
                    :style="{ width: getGreyWidth(rate, a) }"
                ></div>
            </div>
        </div>
        <span class="rate-num">{{ rate }}</span>
    </div>
</template>
<script lang="ts">
import { defineComponent } from 'vue'
import { Icon } from 'vant'
export default defineComponent({
    name:"mask",
    components:{
        [Icon.name]: Icon,
    },
    props:{
        rate:{
            type: Number,
            default: 1
        }
    },
    setup:(props, context)=>{
        const getGreyWidth = (rate:number, currentN:number)=> {
            const w = ((currentN - rate) / 1) * 12
            return w > 12 ? '12px' : `${w}px`
        }
        return {
            getGreyWidth
        }
    }
})
</script>
<style lang="less">
.rate-box{
    display: flex;
    align-items: center;
    justify-content: center;
}
.rate-star-list {
    display: flex;
    align-items: center;
    margin-right: 4px;
    .rate-item {
        display: flex;
        align-items: center;
        justify-content: center;
        position: relative;
        width: 12px;
        height: 12px;
        background: #ff7a2d;
        border-radius: 2px;
        // padding: 2px;
        &-icon {
            transform: scale(0.76);
            z-index: 20;
        }
    }
    .rate-item:not(:last-of-type) {
        margin-right: 2px;
    }
    .grey-bg {
        position: absolute;
        // width: 6px;
        height: 12px;
        background: #cccccc;
        border-radius: 2px;
        top: 0;
        right: 0;
        z-index: 10;
    }
}
.rate-num {
    font-size: 12px;
    font-weight: 500;
    color: #ff7a2d;
    line-height: 16px;
    margin-right: 8px;
}

</style>

父组件引用

import Rate from './components/Rate.vue'
components: {
    Rate
}
<Rate :rate="4.5" />
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值