ranking-list.vue 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <template>
  2. <view>
  3. <view class="ranking">
  4. <view class="ranking-item" v-for="(content,index) in content" :key="index">
  5. <view class="name">{{content.name}}</view>
  6. <view class="progress" >
  7. <text :style="{backgroundImage:'linear-gradient(to top right,'+getColor(0,index)+','+getColor(1,index)+')',width:content.width + '%'}"></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. },
  25. methods:{
  26. init(){
  27. if(this.content && this.content.length >0){
  28. this.content = this.content.sort((a,b) => b.num - a.num);
  29. let max = this.content[0].num;
  30. this.content.map((item,index) =>{
  31. item.width = this.computeWidth(max,item.num);
  32. });
  33. this.$emit("updateRanking",this.content)
  34. }
  35. },
  36. computeWidth(max,current){
  37. let num = (current / max) * 100;
  38. return num.toFixed(2);
  39. },
  40. getColor(id,index){
  41. let color = "";
  42. switch(index){
  43. case 0:
  44. color = id == 0 ? "#fff":"#A80BFC";
  45. break;
  46. case 1:
  47. color = id == 0 ? "#fff":"#740CFF";
  48. break;
  49. case 2:
  50. color = id == 0 ? "#fff":"#4A08DC";
  51. break;
  52. default :
  53. color = id == 0 ? "#fff":"#1936DA";
  54. break;
  55. }
  56. return color;
  57. },
  58. },
  59. mounted() {
  60. this.init();
  61. }
  62. }
  63. </script>
  64. <style scoped lang="scss">
  65. .ranking-item{
  66. display: flex;
  67. margin-bottom: 13rpx;
  68. padding: 10rpx;
  69. align-content: center;
  70. .name{
  71. padding-right: 10rpx;
  72. color: #868688;
  73. }
  74. .progress{
  75. flex:5;
  76. text-align: left;
  77. padding-right: 10rpx;
  78. text{
  79. display: inline-block;
  80. height: 100%;
  81. }
  82. }
  83. .num{
  84. font-size: 26rpx;
  85. color: #3EB2F5;
  86. }
  87. }
  88. </style>