vue實現簡單的評分組件(五角星、評分)


<template>
  <div class="star">
    <span class="star-item on"></span>
    <span class="star-item off"></span>
    <span class="star-item half"></span>
    <hr>
    <span class="star-item" v-for="(item, index) in starList" :key="index" :class="item"></span>
  </div>
</template>
<script>
export default {
  data() {
    return { score: 3.9 }
  },
  computed: {
    starList: function () {
      let result = []
      const score = Math.floor(this.score * 2) / 2 // 4.7 -> 4.5    4.2 -> 4
      const integer = Math.floor(score)
      const hasDecimal = score % 1 !== 0
      // 先將on添加到list中
      for (let i = 0; i < integer; i++) {
        result.push('on')
      }
      // 將半選添加到list
      if (hasDecimal) result.push('half')
      // 若不滿5個,剩下的添加off直到滿5個為止
      for (let i = result.length; i < 5; i++) {
        result.push('off')
      }
      return result
    }
  }
}
</script>
<style lang="less" scoped>
.star-item {
  display: inline-block;
  width: 50px;
  height: 50px;
  background-size: 50px 50px;
  &.on {
    background-image: url('./star/star-on.png');
  }
  &.off {
    background-image: url('./star/star-off.png');
  }
  &.half {
    background-image: url('./star/star-half.png');
  }
}
</style>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM