tui-progress.vue 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <template>
  2. <view class="tui-progress__box">
  3. <view class="tui-progressbar__bg"
  4. :style="{ height: width + 'rpx', borderRadius: radius, background: backgroundColor }">
  5. <view class="tui-progress__bar"
  6. :style="{ height: width + 'rpx', background: activeColor ,transform:`translate3d(-${translateX},0,0)`,transitionDuration:`${time}s`}">
  7. </view>
  8. </view>
  9. <view class="tui-progress__percent"
  10. :style="{ width: percentWidth + 'rpx', fontSize: size + 'rpx', color: color }" v-if="showInfo">
  11. {{ percentage }}%
  12. </view>
  13. </view>
  14. </template>
  15. <script>
  16. export default {
  17. name: 'tuiProgress',
  18. emits: ['activeend'],
  19. props: {
  20. //百分比 0-100
  21. percent: {
  22. type: [Number, String],
  23. default: 0
  24. },
  25. //右侧是否显示百分比
  26. showInfo: {
  27. type: Boolean,
  28. default: false
  29. },
  30. //圆角大小
  31. radius: {
  32. type: String,
  33. default: '8rpx'
  34. },
  35. //右侧百分比字体大小 rpx
  36. size: {
  37. type: Number,
  38. default: 28
  39. },
  40. //右侧百分比颜色
  41. color: {
  42. type: String,
  43. default: '#333'
  44. },
  45. //右侧百分比宽度
  46. percentWidth: {
  47. type: Number,
  48. default: 96
  49. },
  50. //进度条线条宽度 rpx
  51. width: {
  52. type: Number,
  53. default: 8
  54. },
  55. //已选进度条颜色,可渐变
  56. activeColor: {
  57. type: String,
  58. default: '#5677fc'
  59. },
  60. //未选择的进度条的颜色
  61. backgroundColor: {
  62. type: String,
  63. default: '#EBEBEB'
  64. },
  65. //进度增加1%所需毫秒数
  66. duration: {
  67. type: Number,
  68. default: 15
  69. }
  70. },
  71. watch: {
  72. percent(val) {
  73. this.darwProgress();
  74. }
  75. },
  76. mounted() {
  77. this.darwProgress();
  78. },
  79. data() {
  80. return {
  81. percentage: 0,
  82. translateX: '-100%',
  83. time: 0
  84. };
  85. },
  86. methods: {
  87. darwProgress() {
  88. let percent = Number(this.percent);
  89. percent = percent > 100 ? 100 : percent;
  90. this.time = this.duration * Math.abs(percent - this.percentage) / 1000
  91. if (percent < this.percentage && (this.percentage - percent) > 30) {
  92. //后百分比数大于30时 时间缩短
  93. this.time = this.time / 2
  94. }
  95. setTimeout(() => {
  96. this.$emit('activeend', {});
  97. }, this.time)
  98. this.percentage =percent;
  99. this.translateX = (100 - percent) + '%';
  100. }
  101. }
  102. };
  103. </script>
  104. <style scoped>
  105. .tui-progress__box {
  106. width: 100%;
  107. display: flex;
  108. align-items: center;
  109. }
  110. .tui-progressbar__bg {
  111. width: 100%;
  112. position: relative;
  113. overflow: hidden;
  114. transform: translateZ(0);
  115. }
  116. .tui-progress__bar {
  117. width: 100%;
  118. position: absolute;
  119. left: 0;
  120. top: 0;
  121. z-index: 2;
  122. transform: translateX(-100%);
  123. transition-delay: 0s;
  124. transition-property: transform;
  125. transition-timing-function: linear;
  126. }
  127. .tui-progress__percent {
  128. text-align: center;
  129. }
  130. </style>