vue - 项目之评价星星组件的封装

妖狐艹你老母 2021-10-01 02:16 496阅读 0赞

当我们做的项目涉及到交易评价、电视评价、小说评价等方面的时候,我们大多都会选择根据客户评定的分数来对应显示评价星的选中与否,下面我来分享一下我所封装的评价星组件:

1、封装star组件

  1. <template>
  2. <div class="star" :class="starType">
  3. <span v-for="(itemClass,index) in itemClasses" :class="itemClass" class="star-item" :key="index"></span>
  4. </div>
  5. </template>
  6. <script type="text/ecmascript-6">
  7. const LENGTH = 5;
  8. const CLS_ON = 'on';
  9. const CLS_HALF = 'half';
  10. const CLS_OFF = 'off';
  11. export default {
  12. props: {
  13. size: {
  14. type: Number
  15. },
  16. score: {
  17. type: Number
  18. }
  19. },
  20. computed: {
  21. starType() {
  22. return 'star-' + this.size;
  23. },
  24. itemClasses() {
  25. let result = [];
  26. let score = Math.floor(this.score * 2) / 2;
  27. let hasDecimal = score % 1 !== 0;
  28. let integer = Math.floor(score);
  29. for (let i = 0; i < integer; i++) {
  30. result.push(CLS_ON);
  31. }
  32. if (hasDecimal) {
  33. result.push(CLS_HALF);
  34. }
  35. while (result.length < LENGTH) {
  36. result.push(CLS_OFF);
  37. }
  38. return result;
  39. }
  40. }
  41. };
  42. </script>
  43. <style lang="stylus" rel="stylesheet/stylus">
  44. @import "../../common/stylus/style";
  45. </style>

2、样式文件(这里的样式是使用stylus来写的)

  1. bg-image($url) // 设置图片的大小(实现图片的自适应)
  2. background-image url($url + '@2x.png')
  3. @media (-webkit-min-device-pixel-ratio: 3), (min-device-pixel-ratio: 3)
  4. background-image url($url + '@3x.png')
  5. .star
  6. font-size: 0
  7. .star-item
  8. display: inline-block
  9. background-repeat: no-repeat
  10. &.star-48
  11. .star-item
  12. width: 20px
  13. height: 20px
  14. margin-right: 22px
  15. background-size: 20px 20px
  16. &:last-child
  17. margin-right: 0
  18. &.on
  19. bg-image('../../common/img/star48_on')
  20. &.half
  21. bg-image('../../common/img/star48_half')
  22. &.off
  23. bg-image('../../common/img/star48_off')
  24. &.star-36
  25. .star-item
  26. width: 15px
  27. height: 15px
  28. margin-right: 6px
  29. background-size: 15px 15px
  30. &:last-child
  31. margin-right: 0
  32. &.on
  33. bg-image('../../common/img/star36_on')
  34. &.half
  35. bg-image('../../common/img/star36_half')
  36. &.off
  37. bg-image('../../common/img/star36_off')
  38. &.star-24
  39. .star-item
  40. width: 10px
  41. height: 10px
  42. margin-right: 3px
  43. background-size: 10px 10px
  44. &:last-child
  45. margin-right: 0
  46. &.on
  47. bg-image('../../common/img/star24_on')
  48. &.half
  49. bg-image('../../common/img/star24_half')
  50. &.off
  51. bg-image('../../common/img/star24_off')

文件目录:
在这里插入图片描述

3、使用组件

  1. <template>
  2. <div class="header">
  3. <div class="star-wrapper">
  4. <star :size="48" :score="seller.score"></star>
  5. </div>
  6. </div>
  7. </template>
  8. <script>
  9. import star from 'components/star/star';
  10. export default {
  11. components: {
  12. star
  13. }
  14. };
  15. </script>
  16. <style lang="stylus" rel="stylesheet/stylus">
  17. @import "../../common/stylus/style";
  18. </style>

4、实现情况如下图

在这里插入图片描述

发表评论

表情:
评论列表 (有 0 条评论,496人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Vue封装组件过程

    vue组件的定义 ● 组件(Component)是Vue.js最强大的功能之一 ● 组件可以扩展HTML元素,封装可重用代码 ● 在较高层面上,组件是自定义元素,Vue.