u-line-progress.vue 3.5 KB

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