Vue 星级评分组件

Vue 星级评分组件

描述:其实很早就有想做一个星级评分的组价了,并且想写一些组件的代码库,就是在自己没事的时候造一些轮子,在工作的时候直接 Ctrl + CCtrl + V。别人还在苦苦加班写东西的时候,你已经开开心心下班,该干嘛干嘛去了,是不是很爽,哈哈哈哈…
这里插播一个自己的心得。声明一下我并没有说别人开源的东西不好,或者别人有开源伸手即得的东西干嘛还要自己从新写。个人的体会就是,别人的东西也要用,自己的东西也要写,两者并不冲突,并且是一个相辅相成的关系。只有在用了别人的东西多了,才会发现有哪些东西设计的不好,如果然你自己来自己会怎么做,总结出一套个人的经验出来(其实看多了,很多东西的套路都是差不多的,哈哈),这样日后真的有组团队开发的时候不至于一脸懵逼。
再来说说为什么要写。不知道大家有没有这样的体会,很多时候,看别人的东西(特别是视屏教程),一边对着文档(视屏)看源码,特别容易懂,等过一个几天在回来看的时候,感觉又是新的,又要开从头开始。这里面的最重要原因就是你没有动手做写,并没有把别人的东西转化成为你自己的。所以当你开始动手写的时候,你会发现,原来很多不起眼的东西,他们往往就是突破问题的关键,是你以前无法突破的瓶颈。
说了一大堆,感觉重点就是:自己要多去尝试,多动手去写,探索的这些时间不会白费的,日后总会还回来的,毕竟:磨刀不误砍材工!

步入正题…

移动端星级评分组件

说明:该组件仅限在移动端使用,并且在 Vue 项目中

实现的功能点:

  • 核心:用户点击相应的星星时,能够通过事件的形式,把分数派发出来,只要监听:starMarkChange 事件即可
  • 可以配置星星的数量,默认点亮的个数,是否是只读,等

使用方式

目前组件还没有通过 NPM 等包管理工具发出来,如果有老铁需要尝试,只要创建一个 .vue 的文件,就像是自己写的组件一样通过 import 引入就好。

属性

  • starNum 星星的个数,默认:5,只接受 Number 类型数据
  • defaultRating 默认点亮星星的个数,默认:5,只接受 Number 类型数据
  • readOnly 是否是只读,用户不能点击星星,也不会触发事件,在只做展示的场景会非常有用
  • outIndex 外部传进来的一个标志位,在循环做评分列表,标记外部的索引值的时候会比较有用,这样可以快速定位到某项。他会做为 starMarkChange 事件的的第二个参数

事件

  • starMarkChange 点击星星时派发的事件,派发参数:mark,星星的分数,Number类型,还有外部的索引值,如果外部有绑定的情况
<template>
  <div class="star-phone" @click.stop="clickRating">
    <div
      v-for="(item, index) in starNum"
      :key="index"
      :class="isRating(index)"
      :data-index="index"
    />
  </div>
</template>

<script type="text/ecmascript-6">
export default {
  props: {
    starNum: {
      // 星星的个数
      type: Number,
      default: 5,
    },
    defaultRating: {
      // 默认点亮的个数
      type: Number,
      default: 5,
    },
    outIndex: {
      // 外界传进来的一个标识
      type: Number,
      default: 0,
    },
    readOnly: {
      // 是否是只读,默认不是,拥有点击功能
      type: Boolean,
      default: false,
    },
  },
  data () {
    return {
      rating: this.defaultRating, // 用于控制点亮星星的个数
    }
  },
  methods: {
    isRating (index) {
      ++index
      return index <= this.rating ? 'star-item star-active' : 'star-item'
    },
    clickRating (ev) {
      if (this.readOnly) {
        return
      }

      let mark = parseInt(ev.target.dataset.index) + 1
      this.rating = mark
      this.$emit('starMarkChange', mark, this.outIndex)
    },
  },
}
</script>

<style scoped>
  .star-phone {
    position: relative;
    display: inline-block;
    line-height: 26px;
    -webkit-tap-highlight-color:transparent;
  }
  .star-item {
    display: inline-block;
    width: 36px;
    height: 26px;
    background-image: url('./star.png'); /* 换成图片真实路劲,图片在下面给出 */
    background-clip: content-box;
    background-position: center 0;
    background-repeat: repeat-y;
    cursor: pointer;
    vertical-align: bottom;
    white-space: nowrap;
  }
  .star-item:last-child {
    padding-right: 0;
  }
  .star-active {
    background-position: center 26px;
  }
</style>

星星图片

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的 Vue 星级评分组件的实现: ```html <template> <div class="rating"> <span v-for="(star, index) in stars" :class="['star', star]" :key="index" @click="select(index)"> </span> </div> </template> <script> export default { props: { value: { type: Number, default: 0 }, max: { type: Number, default: 5 } }, data() { return { stars: [] } }, mounted() { this.updateStars() }, methods: { updateStars() { this.stars = [] for (let i = 0; i < this.max; i++) { this.stars.push(i < this.value) } }, select(index) { this.value = index + 1 this.$emit('input', this.value) this.updateStars() } } } </script> <style> .star { display: inline-block; width: 24px; height: 24px; background: url(star.png) no-repeat; background-size: contain; cursor: pointer; } .star:hover, .star.active { background-position: 0 -24px; } </style> ``` 使用方式: ```html <template> <div> <h3>我的评分:{{ rating }}</h3> <star-rating v-model="rating"></star-rating> </div> </template> <script> import StarRating from './StarRating.vue' export default { components: { StarRating }, data() { return { rating: 3 } } } </script> ``` 其中,star.png 是一张包含两个星星的图片,第一个星星是默认状态,第二个星星是激活状态。 这个组件的实现很简单,通过 props 接收评分值和最大值,然后用一个数组来表示星星的激活状态,点击某个星星时更新评分值和星星状态,通过事件向父组件传递更新后的评分值。最后,用 CSS 来实现星星的样式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值