tm-propress.vue 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. <template>
  2. <view class="tm-propress" :style="{
  3. width: width_c ,
  4. }">
  5. <view
  6. class="tm-propress-wk "
  7. :style="{
  8. height: height_upx,
  9. overflow:loading?'hidden':'inherit'
  10. }"
  11. >
  12. <view :class="[shape=='round'?'round-24':'',bgColor,black_tmeme?'bk':'']" class="tm-propress-wk-statick "></view>
  13. <view :animation="animationData" :class="[black_tmeme?'bk':'',shape=='round'?'round-24':'',colors.theme?colors.color:'',loading?'ani':'']"
  14. class="tm-propress-wk-active flex-end"
  15. :style="{
  16. height: height_upx,
  17. width:baifenbi,
  18. background:colors.theme?'default':colors.color
  19. }"
  20. >
  21. <block v-if="value>0&&showValue">
  22. <slot name="default" :value="value">
  23. <view class="tm-propress-wk-active-label flex-center px-10 round-24"
  24. :class="[black_tmeme?'bk':'',colors.theme?colors.color:'']"
  25. :style="{background:colors.theme?'default':colors.color}">
  26. <text>{{value}}%</text>
  27. </view>
  28. </slot>
  29. </block>
  30. </view>
  31. </view>
  32. </view>
  33. </template>
  34. <script>
  35. /**
  36. * 进度条
  37. * @property {Function} change value变化时触发.
  38. * @property {Function} input value变化时触发.
  39. * @property {Number} value = [] 通过value来赋值进度,如果想双向绑定需要value.sync,建议使用v-model.
  40. * @property {Number} width = [] 宽度,默认自动宽度,单位rpx,也可输入百分比或者其它类型比如5px
  41. * @property {Number} height = [] 高度,默认6,单位upx,也可输入百分比或者其它类型比如5px
  42. * @property {String} shape = [round|square] 默认round,方形:square,半圆形:round.
  43. * @property {String} color = [] 默认primary,可以是16进制,rgb,rgba,主题色名称.
  44. * @property {String} bg-color = [] 默认 grey-lighten-4 ,背景颜色
  45. * @property {Boolean} loading = [true|false] 默认false,自动加载中...
  46. * @example <tm-propress v-model="checked" ></tm-propress>
  47. */
  48. export default {
  49. name: 'tm-propress',
  50. model:{
  51. prop:"value",
  52. event:"input"
  53. },
  54. props: {
  55. // 单位upx
  56. width: {
  57. type: Number|String,
  58. default: 0
  59. },
  60. // 单位upx
  61. height: {
  62. type: Number|String,
  63. default: 8
  64. },
  65. value:{
  66. type:Number,
  67. default:0
  68. },
  69. showValue:{
  70. type:Boolean|String,
  71. default:true,
  72. },
  73. // 方形:square,半圆形:round.
  74. shape:{
  75. type:String,
  76. default:"round"
  77. },
  78. color:{
  79. type:String,
  80. default:"primary"
  81. },
  82. loading:{
  83. type:Boolean,
  84. default:false
  85. },
  86. bgColor:{
  87. type:String,
  88. default:'grey-lighten-4'
  89. },
  90. // 跟随主题色的改变而改变。
  91. fllowTheme:{
  92. type:Boolean|String,
  93. default:true
  94. },
  95. black: {
  96. type: Boolean,
  97. default: null
  98. },
  99. },
  100. computed: {
  101. black_tmeme: function() {
  102. if (this.black !== null) return this.black;
  103. return this.$tm.vx.state().tmVuetify.black;
  104. },
  105. colors:function(){
  106. return this.$TestColor(this.color_tmeme);
  107. },
  108. color_tmeme:function(){
  109. if(this.$tm.vx.state().tmVuetify.color!==null&&this.$tm.vx.state().tmVuetify.color && this.fllowTheme){
  110. return this.$tm.vx.state().tmVuetify.color;
  111. }
  112. return this.color;
  113. },
  114. width_upx: {
  115. get:function(){
  116. return this.width_c;
  117. },
  118. set:function(val){
  119. if(val==''||val==0||typeof val === 'undefined'){
  120. this.width_c = '100%';
  121. return;
  122. }
  123. let reg = /(vw|vh|rem|em|\%|upx|rpx|auto|px)/g;
  124. if (reg.test(val)) {
  125. this.width_c = val;
  126. return
  127. }
  128. this.width_c = val+'rpx';
  129. }
  130. },
  131. height_upx: function() {
  132. let reg = /(vw|vh|rem|em|\%|upx|rpx|auto|px)/g;
  133. if (reg.test(this.height)) {
  134. return this.height;
  135. }
  136. return this.height+'rpx';
  137. },
  138. baifenbi:function(){
  139. if(this.loading) return "100rpx";
  140. let bl = this.value ;
  141. if(typeof bl !=='number') bl =0;
  142. if(bl>=100) bl =100;
  143. if(bl<=0) bl =0;
  144. this.$emit("input",bl);
  145. this.$emit("update:value",bl);
  146. this.$emit("change",bl);
  147. return bl + "%"
  148. }
  149. },
  150. data() {
  151. return {
  152. width_c:0,
  153. animationData:"",
  154. animation:null,
  155. tmiddd:55656
  156. };
  157. },
  158. created() {
  159. this.width_upx = this.width;
  160. },
  161. destroyed() {
  162. clearInterval(this.tmiddd)
  163. },
  164. async mounted() {
  165. this.$nextTick(async function(){
  166. let p = await this.$Querey(".tm-propress",this).catch(e=>{})
  167. this.width_upx = p[0].width+'px';
  168. if(this.loading){
  169. await this.startAni();
  170. }
  171. })
  172. },
  173. methods: {
  174. async startAni(){
  175. // clearInterval(this.tmiddd)
  176. let t = this;
  177. var animation = uni.createAnimation({
  178. duration: 1000,
  179. timingFunction: 'linear',
  180. })
  181. this.animation = animation
  182. animation.opacity(0).translateX(t.width_c).step()
  183. this.animationData = animation.export()
  184. this.tmiddd = setInterval(async function() {
  185. t.animationData = ""
  186. animation.opacity(1).translateX(0).step({duration:0})
  187. t.animationData = animation.export()
  188. await uni.$tm.sleep(50)
  189. animation.opacity(0).translateX(t.width_c).step()
  190. t.animationData = animation.export()
  191. }.bind(this), 1000)
  192. }
  193. },
  194. };
  195. </script>
  196. <style lang="scss" scoped>
  197. .tm-propress {
  198. .tm-propress-wk {
  199. position: relative;
  200. .tm-propress-wk-statick,
  201. .tm-propress-wk-active {
  202. height: 100%;
  203. }
  204. .tm-propress-wk-active {
  205. position: absolute;
  206. top: 0;
  207. left: 0;
  208. transition:0.35s;
  209. .tm-propress-wk-active-label{
  210. height: 30rpx;
  211. font-size:22rpx;
  212. max-width: 100rpx;
  213. }
  214. // &.ani{
  215. // transition:1s;
  216. // animation-name: linef;
  217. // animation-duration: 1s;
  218. // animation-timing-function: linear;
  219. // animation-iteration-count: infinite;
  220. // }
  221. }
  222. }
  223. }
  224. @keyframes linef{
  225. 0%{
  226. // left:0;
  227. transform: translateX(0);
  228. opacity: 0.4;
  229. }
  230. 100%{
  231. // left:100%;
  232. transform: translateX(311px);
  233. opacity: 1;
  234. }
  235. }
  236. </style>