tm-propress.vue 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. mounted() {
  165. this.getRect()
  166. },
  167. updated() {
  168. this.getRect()
  169. },
  170. methods: {
  171. getRect(){
  172. let t =this
  173. this.$Querey(".tm-propress",this,0).then((p)=>{
  174. if(!p) return;
  175. if(!p[0].width) return;
  176. t.width_upx = p[0].width+'px';
  177. if(t.loading){
  178. t.startAni();
  179. }
  180. }).catch(e=>{})
  181. },
  182. async startAni(){
  183. // clearInterval(this.tmiddd)
  184. let t = this;
  185. var animation = uni.createAnimation({
  186. duration: 1000,
  187. timingFunction: 'linear',
  188. })
  189. this.animation = animation
  190. animation.opacity(0).translateX(t.width_c).step()
  191. this.animationData = animation.export()
  192. this.tmiddd = setInterval(async function() {
  193. t.animationData = ""
  194. animation.opacity(1).translateX(0).step({duration:0})
  195. t.animationData = animation.export()
  196. await uni.$tm.sleep(50)
  197. animation.opacity(0).translateX(t.width_c).step()
  198. t.animationData = animation.export()
  199. }.bind(this), 1000)
  200. }
  201. },
  202. };
  203. </script>
  204. <style lang="scss" scoped>
  205. .tm-propress {
  206. .tm-propress-wk {
  207. position: relative;
  208. .tm-propress-wk-statick,
  209. .tm-propress-wk-active {
  210. height: 100%;
  211. }
  212. .tm-propress-wk-active {
  213. position: absolute;
  214. top: 0;
  215. left: 0;
  216. transition:0.35s;
  217. .tm-propress-wk-active-label{
  218. height: 30rpx;
  219. font-size:22rpx;
  220. max-width: 100rpx;
  221. }
  222. // &.ani{
  223. // transition:1s;
  224. // animation-name: linef;
  225. // animation-duration: 1s;
  226. // animation-timing-function: linear;
  227. // animation-iteration-count: infinite;
  228. // }
  229. }
  230. }
  231. }
  232. @keyframes linef{
  233. 0%{
  234. // left:0;
  235. transform: translateX(0);
  236. opacity: 0.4;
  237. }
  238. 100%{
  239. // left:100%;
  240. transform: translateX(311px);
  241. opacity: 1;
  242. }
  243. }
  244. </style>