progress-loader.vue 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <template>
  2. <view class="">
  3. <view class="progress-loader" ref="wrapper">
  4. <text class="_text-bold" :style="{ color: underColor, fontSize: fontSize + 'rpx' }">{{ text }}</text>
  5. <view class="progress-under" :class="{'progress-hidden': initting}" :style="{ color: highlightColor }" ref="progressUnder">
  6. <text class="_text-bold" :style="{ color: highlightColor, fontSize: fontSize + 'rpx' }">{{ text }}</text>
  7. </view>
  8. </view>
  9. </view>
  10. </template>
  11. <script>
  12. // ls-loading组件内置动画效果组件: 模拟进度条动画效果
  13. // #ifdef APP-NVUE
  14. const animation = weex.requireModule('animation');
  15. const dom = weex.requireModule('dom');
  16. // #endif
  17. let sit;
  18. export default {
  19. name: 'progress-loader',
  20. props: {
  21. // 加载中的文字
  22. text: {
  23. type: String,
  24. default: '正在加载'
  25. },
  26. // 加载中文字大小
  27. fontSize: {
  28. type: [String, Number],
  29. default: 58
  30. },
  31. // 底层颜色
  32. underColor: {
  33. type: String,
  34. default: '#e8e7e8'
  35. },
  36. // 高亮部分颜色
  37. highlightColor: {
  38. type: String,
  39. default: '#00bfff'
  40. }
  41. },
  42. data() {
  43. return {
  44. // 容器初始化
  45. // nvue初始化时,容器需要有宽度。 为了保证文字最初显示底色,初始化时给高亮色部分先隐藏
  46. initting: true
  47. }
  48. },
  49. mounted() {
  50. // #ifdef APP-NVUE
  51. // 加个延时,保证一定可以获取到文字容器的宽度
  52. setTimeout(() => {
  53. this.createAnimation();
  54. }, 100);
  55. // #endif
  56. // #ifndef APP-NVUE
  57. this.initting = false;
  58. // #endif
  59. },
  60. destroyed() {
  61. clearInterval(sit);
  62. },
  63. methods: {
  64. createAnimation() {
  65. // 获取文字容器宽度
  66. const result = dom.getComponentRect(this.$refs.wrapper, option => {
  67. const width = option.size.width + 'px';
  68. this.levelReset();
  69. // 初始化完毕,显示高亮文字
  70. setTimeout(() => {
  71. this.initting = false;
  72. }, 40);
  73. this.executeAnimation(width);
  74. });
  75. },
  76. executeAnimation(width) {
  77. this.progressLeval(width);
  78. let timer = 0;
  79. clearInterval(sit);
  80. sit = setInterval(() => {
  81. if (timer == 3) {
  82. this.levelReset();
  83. timer = 0;
  84. } else {
  85. if (timer == 0) {
  86. this.progressLeval(width);
  87. }
  88. timer++;
  89. }
  90. }, 1000);
  91. },
  92. // 进度重置
  93. levelReset(width = '0px') {
  94. animation.transition(this.$refs.progressUnder, {
  95. styles: {
  96. width,
  97. },
  98. duration: 0, //ms
  99. timingFunction: 'linear',
  100. needLayout: false,
  101. delay: 0 //ms
  102. });
  103. },
  104. // 进度动画
  105. progressLeval(width) {
  106. animation.transition(this.$refs.progressUnder, {
  107. styles: {
  108. width
  109. },
  110. duration: 3000, //ms
  111. timingFunction: 'linear',
  112. needLayout: false,
  113. delay: 0 //ms
  114. });
  115. }
  116. }
  117. };
  118. </script>
  119. <style scoped>
  120. .progress-loader {
  121. position: relative;
  122. flex: 1;
  123. }
  124. .progress-under {
  125. position: absolute;
  126. left: 0;
  127. top: 0;
  128. bottom: 0;
  129. overflow: hidden;
  130. /* #ifdef APP-NVUE */
  131. lines: 1;
  132. width: 750rpx;
  133. /* #endif */
  134. /* #ifndef APP-NVUE */
  135. width: 0;
  136. white-space: nowrap;
  137. animation-name: progressloader;
  138. animation-delay: 0.5s;
  139. animation-timing-function: linear;
  140. animation-duration: 3s;
  141. animation-iteration-count: infinite;
  142. /* #endif */
  143. }
  144. ._text-bold {
  145. font-weight: 700;
  146. lines: 1;
  147. }
  148. .progress-hidden {
  149. opacity: 0;
  150. }
  151. /* #ifndef APP-NVUE */
  152. @keyframes progressloader {
  153. 0% {
  154. width: 0%;
  155. }
  156. 100% {
  157. width: 100%;
  158. }
  159. }
  160. /* #endif */
  161. </style>