progress-bar.vue 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <template>
  2. <view>
  3. <view v-if="copyContent.length > 0" class="ranking">
  4. <view class="ranking-item" v-for="(content,index) in copyContent" :key="index" :style="{padding:progressPadding+'rpx'}">
  5. <view class="name">{{content.name}}</view>
  6. <view class="progress" >
  7. <text :style="{background:content.background,width:content.width + '%',height:progressWidth+'rpx'}"></text>
  8. </view>
  9. <view class="num">{{content.num}}</view>
  10. </view>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. export default{
  16. name:'ranking-list',
  17. props:{
  18. content:{
  19. type: Array,
  20. default() {
  21. return []
  22. }
  23. },
  24. isPC:{
  25. type:Boolean,
  26. default:false
  27. },
  28. isRank:{
  29. type:Boolean,
  30. default:false
  31. }
  32. },
  33. data(){
  34. return{
  35. progressWidth:24,
  36. progressPadding:10,
  37. maxNumber:0,
  38. culCount:0,
  39. copyContent:[]
  40. }
  41. },
  42. watch:{
  43. content(newV){
  44. this.init()
  45. }
  46. },
  47. methods:{
  48. init(){
  49. this.copyContent = this.deepClone(this.content)
  50. if(this.copyContent && this.copyContent.length >0){
  51. if(this.isRank){
  52. this.copyContent = this.copyContent.sort((a,b) => b.num - a.num);
  53. this.maxNumber = this.copyContent[0].num;
  54. }else{
  55. this.maxNumber = Math.max.apply(Math,this.copyContent.map(item => { return item.num }));
  56. }
  57. this.copyContent.map((item,index) =>{
  58. item.width = this.computeWidth(this.maxNumber,item.num);
  59. });
  60. }
  61. },
  62. computeWidth(max,current){
  63. let num = (current / max) * 100;
  64. return num.toFixed(2);
  65. },
  66. deepClone(obj) {
  67. var cloneObj = new obj.constructor()
  68. if(obj === null) return obj
  69. if(obj instanceof Date) return new Date(obj)
  70. if(obj instanceof RegExp) return new RegExp(obj)
  71. if (typeof obj !== 'object') return obj
  72. for (var i in obj) {
  73. if (obj.hasOwnProperty(i)) {
  74. cloneObj[i] = this.deepClone(obj[i])
  75. }
  76. }
  77. return cloneObj
  78. }
  79. },
  80. mounted() {
  81. if(this.isPC){
  82. this.progressWidth = 40;
  83. this.progressPadding = 30;
  84. }
  85. this.init();
  86. }
  87. }
  88. </script>
  89. <style scoped lang="scss">
  90. .ranking-item{
  91. display: flex;
  92. margin-bottom: 13rpx;
  93. align-content: center;
  94. height: 50rpx;
  95. .name{
  96. padding-right: 10rpx;
  97. color: #868688;
  98. font-size: 20rpx;
  99. flex: 1;
  100. white-space: nowrap;
  101. overflow: hidden;
  102. text-overflow: ellipsis;
  103. }
  104. .progress{
  105. flex:5;
  106. text-align: left;
  107. padding-right: 10rpx;
  108. text{
  109. display: inline-block;
  110. border-radius: 30rpx;
  111. vertical-align:top;
  112. }
  113. }
  114. .num{
  115. font-size: 26rpx;
  116. color: #3EB2F5;
  117. flex: 1;
  118. }
  119. }
  120. </style>