ai-progress.vue 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. <template>
  2. <view>
  3. <view class="flex a-center content" v-if="lineData">
  4. <view><slot name="content">{{content}}</slot></view>
  5. </view>
  6. <view class="flex a-center" style="padding-right: 10rpx;">
  7. <view class="progress-container" id="container" ref="progressContainer" :style="{ background: inBgColor }">
  8. <view
  9. class="progress-content flex j-end"
  10. id="content"
  11. ref="progressContent"
  12. :style="{ height: strokeWidth + 'px', background: bgColor, width: contentWidth, transition: `width ${duration / 1000}s ease` }"
  13. v-if="isAnimate"
  14. >
  15. <view class="textInside flex a-center j-center" v-if="textInside && !noData">
  16. <view>{{ percentage }}%</view>
  17. </view>
  18. </view>
  19. <view v-if="!isAnimate" class="progress-content flex j-end" :style="{ width: percentage + '%', height: strokeWidth + 'px', background: bgColor }">
  20. <view class="textInside flex a-center j-center" v-if="textInside && !noData">
  21. <view class="text">{{ percentage }}%</view>
  22. </view>
  23. </view>
  24. </view>
  25. <view>
  26. <view class="percentage" v-if="!textInside && !lineData && !noData && !isAnimate">{{ percentage }}%</view>
  27. </view>
  28. </view>
  29. </view>
  30. </template>
  31. <script>
  32. export default {
  33. name: 'AiProgress',
  34. components: {},
  35. props: {
  36. // 进度条的值
  37. percentage: {
  38. type: [Number, String],
  39. required: true
  40. },
  41. // 是否内联显示数据
  42. textInside: {
  43. type: Boolean,
  44. default: false
  45. },
  46. // 进度条高度
  47. strokeWidth: {
  48. type: [Number, String],
  49. default: 6
  50. },
  51. // 默认动画时长
  52. duration: {
  53. type: [Number, String],
  54. default: 2000
  55. },
  56. // 是否有动画
  57. isAnimate: {
  58. type: Boolean,
  59. default: false
  60. },
  61. // 背景颜色
  62. bgColor: {
  63. type: String,
  64. default: '#409eff'
  65. },
  66. content: {
  67. type: String,
  68. default: ''
  69. },
  70. // 是否不显示数据
  71. noData: {
  72. type: Boolean,
  73. default: false
  74. },
  75. // 是否自定义显示内容
  76. lineData: {
  77. type: Boolean,
  78. default: false
  79. },
  80. // 自定义底色
  81. inBgColor: {
  82. type: String,
  83. default: '#ebeef5'
  84. }
  85. },
  86. data() {
  87. return {
  88. width: 0,
  89. timer: null,
  90. containerWidth: 0,
  91. contentWidth: 0
  92. };
  93. },
  94. methods: {
  95. start() {
  96. if (this.isAnimate) {
  97. // #ifdef H5
  98. this.$nextTick(() => {
  99. let progressContainer = this.$refs.progressContainer.$el;
  100. let progressContent = this.$refs.progressContent.$el;
  101. let style = window.getComputedStyle(progressContainer, null);
  102. let width = style.width.replace('px', '') * ((this.percentage * 1) / 100);
  103. progressContent.style.width = width.toFixed(2) + 'px';
  104. progressContent.style.transition = `width ${this.duration / 1000}s ease`;
  105. });
  106. // #endif
  107. const container = uni
  108. .createSelectorQuery()
  109. .in(this)
  110. .selectAll('#container');
  111. const content = uni
  112. .createSelectorQuery()
  113. .in(this)
  114. .selectAll('#content');
  115. container.boundingClientRect().exec(res1 => {
  116. this.contentWidth = res1[0][0].width * 1 * ((this.percentage * 1) / 100).toFixed(2) + 'px';
  117. });
  118. }
  119. }
  120. },
  121. mounted() {
  122. this.$nextTick(() => {
  123. this.start();
  124. });
  125. },
  126. created() {},
  127. filters: {},
  128. computed: {},
  129. watch: {},
  130. directives: {}
  131. };
  132. </script>
  133. <style scoped lang="scss">
  134. .content {
  135. margin-bottom: 10px;
  136. .c-per {
  137. font-size: 26px;
  138. }
  139. }
  140. .progress-container {
  141. width: 100%;
  142. border-radius: 100px;
  143. .progress-content {
  144. border-radius: 100px;
  145. width: 0;
  146. }
  147. .textInside {
  148. color: #fff;
  149. margin-right: 10rpx;
  150. position: relative;
  151. }
  152. }
  153. .text {
  154. margin-left: 10rpx;
  155. }
  156. .percentage {
  157. margin-left: 6px;
  158. font-size: 12px;
  159. width: 30px;
  160. }
  161. .flex {
  162. display: flex;
  163. }
  164. .a-center {
  165. align-items: center;
  166. }
  167. .j-center {
  168. justify-content: center;
  169. }
  170. .j-between {
  171. justify-content: space-between;
  172. }
  173. .content {
  174. margin-bottom: 10px;
  175. color: #666;
  176. font-size: 32rpx;
  177. }
  178. </style>