美文网首页
vue 星星评分组件

vue 星星评分组件

作者: 瓜田猹 | 来源:发表于2017-08-21 14:30 被阅读830次
star.png

一、组件(star.vue)代码

<template>
  <div>
    <div class="star mb-10" :class="starType">
        <span class="star-item" v-for = "itemClass in itemClassess" :class="itemClass" track-by="$index"></span>
    </div>
  </div>
</template>

<script>
     const LENGTH = 5;//星星个数
     const CLS_ON = "on";//满星状态
     const CLS_HALF = "half";//半星状态
     const CLS_OFF = "off";//无星状态

    export default {
         props: {
            size: {
                type : Number//参数:尺寸
            },
            score: {
                type : Number//参数:评分
            }
         },
         computed: {
            starType(){//设置星星尺寸
                return "star-" + this.size;
            },
            itemClassess(){
                let result = [];//记录状态的数组
                let score = Math.floor(this.score * 2) / 2;
                let hasDecimal = score % 1 !==0;
                let integer = Math.floor(score);//向下取整
                //全星
                for(let i = 0; i < integer; i++){
                    result.push(CLS_ON);
                }
                //半星
                if(hasDecimal){
                    result.push(CLS_HALF);
                }
                //无星
                if(result.length < LENGTH){
                    result.push(CLS_OFF);
                }
                return result;
            }
         }
    }
</script>

<style>
    .star {
        display: flex;
        width: 100%;
    }
    .star-48 .star-item { 
        width: 20px;
        height: 20px;
        margin-right: 22px;
        background-repeat: no-repeat;
        background-size: 20px 20px;
    }
    .star-48 .star-item:last-child {
        margin-right: 0px;
    }
    .star-48 .star-item.on {
        background-image: url("./on.png");
    }
    .star-48 .star-item.half {
        background-image: url("./half.png");
    }
    .star-48 .star-item.off {
        background-image: url("./off.png");
    }

    .star-36 .star-item { 
        width: 15px;
        height: 15px;
        margin-right: 6px;
        background-repeat: no-repeat;
        background-size: 15px 15px;
    }
    .star-36 .star-item:last-child {
        margin-right: 0px;
    }
    .star-36 .star-item:last-child {
        margin-right: 0px;
    }
    .star-36 .star-item.on {
        background-image: url("./on.png");
    }
    .star-36 .star-item.half {
        background-image: url("./half.png");
    }
    .star-36 .star-item.off {
        background-image: url("./off.png");
    }
</style>

二、页面调用此组件( star.vue ):

<template>
     <star :size="48" :score="4.5"></star>
</template>
<script>
    import star from "../../components/star/star.vue";
        export default {
        components: {
           star
        }
    }
</script>


相关文章

  • vue 星星评分组件

    评分插件在购物的应用中经常可以看得到,但是用着别人的总是没有自己写的顺手,正好趁着这段时间做一个移动端应用的时候写...

  • vue 星星评分组件

    一、组件(star.vue)代码 二、页面调用此组件( star.vue ):

  • 评分组件(星星)

    1.文字型(不带半星) html结构 css样式 js代码 2.图片型(带半星) html结构 css样式 js代码

  • Vue版本评分组件

    实现思路:使用实心星以绝对定位的方式覆盖在空心星的上面,利用.high-light的em动态宽度,配合使用over...

  • Flutter学习笔记 -- 5星评级组件

    Flutter实现: 5星评分的展示的小组件 -- 支持不同数量的星星、颜色、大小等

  • 自定义评分条(实现方式一)-CustomRatingBar

    Android原生的RatingBar是一个评分组件,但是局限性比较多,像星星大小不好调整,星星之间的间距不好调整...

  • Vue3.0 父子组件传值方法

    父组件.vue 子组件.vue

  • provide inject在vue2和vue3中的使用

    vue2父组件 vue2子组件 vue3父组件 vue3子组件 vue3官方详细使用provide inject地...

  • 星星评分

    关于评价星级的基本原理 主要根据鼠标滑过改变元素的背景图片造成评价的结果。 评价 控制台输出了slice,spli...

  • 星星评分

    星星评分 我们很多时候 为了 UI界面的 简便 美观 ,时常用到 星级评分 1.实现思路 2.代码实现 .h 文件...

网友评论

      本文标题:vue 星星评分组件

      本文链接:https://www.haomeiwen.com/subject/nhycdxtx.html