Item.vue 2.8 KB

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