Item.vue 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <template>
  2. <div class="count-item">
  3. <span class="title">{{ title }}</span>
  4. <div class="count-content">
  5. <span v-for="num in nums" :class="num == ',' ? 'separator' : num == 0 ? 'zero num' : 'num'">{{ num }}</span>
  6. <!-- <span v-if="nums.length < 7">个</span>
  7. <span v-if="nums.length > 7">万</span>-->
  8. <span v-text="isMore?'万':'个'"></span>
  9. </div>
  10. </div>
  11. </template>
  12. <script>
  13. export default {
  14. name: 'count-item',
  15. props: {
  16. value: {
  17. default: 0,
  18. type: Number
  19. },
  20. title: {
  21. type: String
  22. }
  23. },
  24. data () {
  25. return {
  26. isMore: false
  27. }
  28. },
  29. methods: {
  30. formatNumber (num) {
  31. console.log(num)
  32. let re = /(\d+)(\d{3})/
  33. if (num > 9999) {
  34. this.isMore = true
  35. let str = num.toString()
  36. num = Math.floor(num / 10000)
  37. if (parseInt(str.charAt(str.length - 4)) > 4) {
  38. num = num + 1
  39. }
  40. }
  41. num = (Array(7 - String(num).length).join(0) + num)
  42. while (re.test(num)) {
  43. num = num.replace(re, '$1,$2')
  44. }
  45. num = num.split('')
  46. // console.log(num)
  47. return num
  48. }
  49. },
  50. computed: {
  51. nums () {
  52. return this.formatNumber(this.value)
  53. }
  54. }
  55. }
  56. </script>
  57. <style lang="scss" scoped>
  58. @import '~assets/scss/variables';
  59. .count-item {
  60. text-align: center;
  61. line-height: 20px;
  62. .title {
  63. display: inline-block;
  64. width: 58px;
  65. float: left;
  66. font-weight: bold;
  67. line-height: 40px;
  68. color: #fff;
  69. font-size: 14px;
  70. }
  71. .separator, .num {
  72. display: inline-block;
  73. }
  74. .separator {
  75. font-size: 12px;
  76. color: #6cb7f3;
  77. line-height: 38px !important;
  78. margin: 0 5px 0 -5px;
  79. width: 3px;
  80. }
  81. .count-content{
  82. width: 145px;
  83. background: #fff;
  84. float: right;
  85. height: 30px;
  86. margin-top: 5px;
  87. margin-right: 5px;
  88. span{
  89. float: left;
  90. line-height: 24px;
  91. font-weight: bold;
  92. }
  93. span.zero{
  94. color: #bbdffb;
  95. }
  96. span:first-child{
  97. margin-left: 3px;
  98. }
  99. span:last-child{
  100. line-height: 30px;
  101. color: #6cb7f3;
  102. margin-left: 2px;
  103. }
  104. }
  105. .num {
  106. background: #9dd0f9;
  107. width: 18px;
  108. height: 24px;
  109. margin-right: 2px;
  110. line-height: 24px;
  111. text-align: center;
  112. color: $white;
  113. font-weight: bold;
  114. border-radius: 2px;
  115. margin-top: 3px;
  116. }
  117. }
  118. </style>