progress-bar.vue 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. <template>
  2. <view class="progress-wrap">
  3. <view class="progress-track" :style="{ background: trackBg }">
  4. <view class="progress-fill" :style="{ width: safePercent + '%', background: fillColor }"></view>
  5. </view>
  6. </view>
  7. </template>
  8. <script>
  9. /**
  10. * 线性进度条(原子组件,纯 flexbox,不用 CSS Grid)
  11. * 用法:<ProgressBar :percent="68" />
  12. */
  13. export default {
  14. name: 'ProgressBar',
  15. props: {
  16. // 0-100
  17. percent: {
  18. type: Number,
  19. default: 0
  20. },
  21. color: {
  22. type: String,
  23. default: '#2E6FA8'
  24. },
  25. trackBg: {
  26. type: String,
  27. default: '#E8F3FC'
  28. }
  29. },
  30. computed: {
  31. safePercent() {
  32. var p = Number(this.percent) || 0
  33. if (p < 0) return 0
  34. if (p > 100) return 100
  35. return p
  36. },
  37. fillColor() {
  38. return this.color || '#2E6FA8'
  39. }
  40. }
  41. }
  42. </script>
  43. <style scoped>
  44. .progress-wrap {
  45. width: 100%;
  46. }
  47. .progress-track {
  48. width: 100%;
  49. height: 18rpx;
  50. border-radius: 10rpx;
  51. overflow: hidden;
  52. display: flex;
  53. align-items: stretch;
  54. }
  55. .progress-fill {
  56. height: 18rpx;
  57. border-radius: 10rpx;
  58. }
  59. </style>