≡
  • 网络编程
  • 数据库
  • CMS技巧
  • 软件编程
  • PHP笔记
  • JavaScript
  • MySQL
位置:首页 > 网络编程 > vue.js

vue.js 实现评价五角星组件的实例代码

人气:582 时间:2019-04-11

这篇文章主要为大家详细介绍了vue.js 实现评价五角星组件的实例代码,具有一定的参考价值,可以用来参考一下。

感兴趣的小伙伴,下面一起跟随四海网的小编两巴掌来看看吧!

饿了么的五角星有三种形状,分别是实星,半星,空星

【图片暂缺】

并且组件要能实现,这个五角星不同大小,评分也不一样,比如满分五颗星,四颗半星,四颗星等等....

所以需要像组件传入一个大小:size,一个分数:score

代码如下:

代码如下:


<template>
   <div class="star" :class="starType">
     <span class="star-item" :class="itemClass" v-for="itemClass in itemClasses"></span>
   </div>
 </template>
 <script type="text/ecmascript-6">
   const LENGTH=5;
   const CLS_ON="on";
   const CLS_OFF="off";
   const CLS_HALF="half";
  export default {
     props:{
       size:{
         type:Number
       },
       score:{
         type:Number
       }
     },
     computed:{
       starType(){
         return "star-"+this.size;
       },
       itemClasses(){
         //计算属性
        let result=[];
         let score=Math.floor(this.score*2)/2;
         let hasDecimal=score%1!==0; //是否有效数
        let integer=Math.floor(score);//取整
        for (var i = 0; i < integer; i++) {
           result.push(CLS_ON);
         }
         if (hasDecimal) {
           //有且最多一个半星
          result.push(CLS_HALF);
         }
         while(result.length<LENGTH){
           result.push(CLS_OFF);
         }
         //数组填充完毕
        return result;
       }
     }
   };
 </script>
 <style lang="stylus" ref="sheetstyle/stylus">
   @import "../../common/stylus/mixin.styl";
   .star
     font-size:0
     .star-item
       display:inline-block
       background-repeat:no-repeat
     &.star-48
       .star-item
         width:20px
         height:20px
         margin-right:22px
         background-size:20px 20px
         &:last-child
           margin-right:0
         &.on
           bg-image('star48_on')
         &.half
           bg-image('star48_half')
         &.off
           bg-image('star48_off')
     &.star-36
       .star-item
         width:15px
         height:15px
         margin-right:6px
         background-size:15px 15px
         &:last-child
           margin-right:0
         &.on
           bg-image('star36_on')
         &.half
           bg-image('star36_half')
         &.off
           bg-image('star36_off')
     &.star-24
       .star-item
         width:10px
         height:10px
         margin-right:3px
         background-size:10px 10px
         &:last-child
           margin-right:0
         &.on
           bg-image('star24_on')
         &.half
           bg-image('star24_half')
         &.off
           bg-image('star24_off')  
 </style>

其中:使用了size为48,36,24,所以我们需要图片3种类型的图片,并且要使用适应不同分辨率,要有@2x.png,@3x.png图片

【图片暂缺】

对了,bg-image方法是在mixin.styl中的,代码如下

代码如下:


bg-image($url)
   background-image:url($url+"@2x.png")
   @media (-webkit-min-device-pixel-ratio:3),(min-device-pixel-ratio:3)
     background-image:url($url+"@3x.png")

组件的使用就很简单了

代码如下:


<star :size="48" :score="3.5"></star>

【图片暂缺】

 

总结

 

以上所述是小编给大家介绍的vue.js 实现评价五角星组件的实例代码,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对四海网网站的支持!

本文来自:http://www.q1010.com/184/5868-0.html

注:关于vue.js 实现评价五角星组件的实例代码的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。

关键词:vue.js

您可能感兴趣的文章

  • vuejs前后端数据交互之从后端请求数据的实例
  • axios向后台传递数组作为参数的方法
  • VUE在for循环里面根据内容值动态的加入class值的方法
  • Vue实现左右菜单联动实现代码
  • 深入理解Vue父子组件生命周期执行顺序及钩子函数
  • 解决vue中使用Axios调用接口时出现的ie数据处理问题
  • Vue Promise的axios请求封装分析
  • 让axios发送表单请求形式的键值对post数据的实例
  • Vue中的v-for循环key属性注意事项小结
  • Vue.js实现数据响应的方法
上一篇:vue2.0 中使用transition实现动画效果使用心得
下一篇:Vue Promise的axios请求封装分析
热门文章
  • Vue 报错TypeError: this.$set is not a function 的解决方法
  • vue实现动态添加数据滚动条自动滚动到底部的示例代码
  • vue项目设置scrollTop不起作用(总结)
  • vue项目中使用vue-i18n报错的解决方法
  • iview实现select tree树形下拉框的示例代码
  • 分析关于element级联选择器数据回显问题
  • vue项目打包后打开页面空白解决办法
  • 解决element ui select下拉框不回显数据问题的解决
  • element-ui table span-method(行合并)的实现代码
  • element-ui 设置菜单栏展开的方法
  • 最新文章
    • 理解vue ssr原理并自己搭建简单的ssr框架
    • vue favicon设置以及动态修改favicon的方法
    • vue-router启用history模式下的开发及非根目录部署方法
    • 从零开始在NPM上发布一个Vue组件的方法步骤
    • Element input树型下拉框的实现代码
    • Vue 报错TypeError: this.$set is not a function 的解决方法
    • Vue.js组件高级特性实例分析
    • 浅谈VueJS SSR 后端绘制内存泄漏的相关解决经验
    • 分析Vue.js自定义tipOnce指令用法实例
    • 浅谈vuex actions和mutation的异曲同工

四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。