You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

149 lines
3.5 KiB

2 months ago
  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. return style
  65. },
  66. innserPercentage() {
  67. // 控制范围在0-100之间
  68. return range(0, 100, this.percentage)
  69. }
  70. },
  71. mounted() {
  72. this.init()
  73. },
  74. methods: {
  75. addStyle,
  76. addUnit,
  77. init() {
  78. sleep(20).then(() => {
  79. this.resizeProgressWidth()
  80. })
  81. },
  82. getProgressWidth() {
  83. // #ifndef APP-NVUE
  84. return this.$uGetRect('.u-line-progress__background')
  85. // #endif
  86. // #ifdef APP-NVUE
  87. // 返回一个promise
  88. return new Promise(resolve => {
  89. dom.getComponentRect(this.$refs['u-line-progress__background'], (res) => {
  90. resolve(res.size)
  91. })
  92. })
  93. // #endif
  94. },
  95. resizeProgressWidth() {
  96. this.getProgressWidth().then(size => {
  97. const {
  98. width
  99. } = size
  100. // 通过设置的percentage值,计算其所占总长度的百分比
  101. this.lineWidth = width * this.innserPercentage / 100 + 'px'
  102. })
  103. }
  104. }
  105. }
  106. </script>
  107. <style lang="scss" scoped>
  108. @import "../../libs/css/components.scss";
  109. .u-line-progress {
  110. align-items: stretch;
  111. position: relative;
  112. @include flex(row);
  113. flex: 1;
  114. overflow: hidden;
  115. border-radius: 100px;
  116. &__background {
  117. background-color: #ececec;
  118. border-radius: 100px;
  119. flex: 1;
  120. }
  121. &__line {
  122. position: absolute;
  123. top: 0;
  124. left: 0;
  125. bottom: 0;
  126. align-items: center;
  127. @include flex(row);
  128. color: #ffffff;
  129. border-radius: 100px;
  130. transition: width 0.5s ease;
  131. justify-content: flex-end;
  132. }
  133. &__text {
  134. font-size: 10px;
  135. align-items: center;
  136. text-align: right;
  137. color: #FFFFFF;
  138. margin-right: 5px;
  139. transform: scale(0.9);
  140. }
  141. }
  142. </style>