u-line-progress.vue 3.3 KB

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