u-line-progress.vue 3.4 KB

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