u-line-progress.vue 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <template>
  2. <view class="u-line-progress" :style="[$u.addStyle(customStyle)]">
  3. <view class="u-line-progress__background" ref="u-line-progress__background" :style="[{
  4. backgroundColor: inactiveColor,
  5. height: $u.addUnit(height),
  6. }]">
  7. </view>
  8. <view class="u-line-progress__line" :style="[progressStyle]">
  9. <slot>
  10. <text v-if="showText && percentage >= 10"
  11. class="u-line-progress__text">{{innserPercentage + '%'}}</text>
  12. </slot>
  13. </view>
  14. </view>
  15. </template>
  16. <script>
  17. import props from './props.js';
  18. // #ifdef APP-NVUE
  19. const dom = uni.requireNativePlugin('dom')
  20. // #endif
  21. /**
  22. * lineProgress 线型进度条
  23. * @description 展示操作或任务的当前进度,比如上传文件,是一个线形的进度条。
  24. * @tutorial https://www.uviewui.com/components/lineProgress.html
  25. * @property {String} activeColor 激活部分的颜色 ( 默认 '#19be6b' )
  26. * @property {String} inactiveColor 背景色 ( 默认 '#ececec' )
  27. * @property {String | Number} percentage 进度百分比,数值 ( 默认 0 )
  28. * @property {Boolean} showText 是否在进度条内部显示百分比的值 ( 默认 true )
  29. * @property {String | Number} height 进度条的高度,单位px ( 默认 12 )
  30. *
  31. * @example <u-line-progress :percent="70" :show-percent="true"></u-line-progress>
  32. */
  33. export default {
  34. name: "u-line-progress",
  35. mixins: [uni.$u.mpMixin, uni.$u.mixin, props],
  36. data() {
  37. return {
  38. lineWidth: 0,
  39. }
  40. },
  41. watch: {
  42. percentage(n) {
  43. this.resizeProgressWidth()
  44. }
  45. },
  46. computed: {
  47. progressStyle() {
  48. let style = {}
  49. style.width = this.lineWidth
  50. style.backgroundColor = this.activeColor
  51. style.height = uni.$u.addUnit(this.height)
  52. return style
  53. },
  54. innserPercentage() {
  55. // 控制范围在0-100之间
  56. return uni.$u.range(0, 100, this.percentage)
  57. }
  58. },
  59. mounted() {
  60. this.init()
  61. },
  62. methods: {
  63. init() {
  64. uni.$u.sleep(20).then(() => {
  65. this.resizeProgressWidth()
  66. })
  67. },
  68. getProgressWidth() {
  69. // #ifndef APP-NVUE
  70. return this.$uGetRect('.u-line-progress__background')
  71. // #endif
  72. // #ifdef APP-NVUE
  73. // 返回一个promise
  74. return new Promise(resolve => {
  75. dom.getComponentRect(this.$refs['u-line-progress__background'], (res) => {
  76. resolve(res.size)
  77. })
  78. })
  79. // #endif
  80. },
  81. resizeProgressWidth() {
  82. this.getProgressWidth().then(size => {
  83. const {
  84. width
  85. } = size
  86. // 通过设置的percentage值,计算其所占总长度的百分比
  87. this.lineWidth = width * this.innserPercentage / 100 + 'px'
  88. })
  89. }
  90. }
  91. }
  92. </script>
  93. <style lang="scss">
  94. @import "../../libs/css/components.scss";
  95. .u-line-progress {
  96. align-items: stretch;
  97. position: relative;
  98. @include flex(row);
  99. flex: 1;
  100. &__background {
  101. background-color: #ececec;
  102. border-radius: 100px;
  103. flex: 1;
  104. transition: background-color 3s;
  105. }
  106. &__line {
  107. position: absolute;
  108. top: 0;
  109. left: 0;
  110. bottom: 0;
  111. align-items: center;
  112. @include flex(row);
  113. color: #ffffff;
  114. border-radius: 100px;
  115. transition: width 0.5s ease;
  116. justify-content: flex-end;
  117. }
  118. &__text {
  119. font-size: 10px;
  120. align-items: center;
  121. text-align: right;
  122. color: #FFFFFF;
  123. margin-right: 5px;
  124. transform: scale(0.9);
  125. }
  126. }
  127. </style>