gui-single-slider.vue 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. <template>
  2. <view
  3. class="gui-sg-slider"
  4. @touchstart="touchstart"
  5. @touchmove.stop.prevent="touchmove"
  6. @touchend="touchend"
  7. ref="gracesgslider"
  8. id="gracesgslider"
  9. :style="{
  10. height:barHeight+'rpx'
  11. }">
  12. <view
  13. class="gui-sg-slider-line"
  14. :class="bglineClass"
  15. :style="{
  16. opacity:0.6,
  17. height:bglineSize+'rpx',
  18. marginTop:((barHeight - bglineSize) / 2)+'rpx',
  19. borderRadius:borderRadius
  20. }"></view>
  21. <view
  22. class="gui-sg-slider-a-line"
  23. :class="bglineAClass"
  24. :style="{
  25. width:(left+25)+'px',
  26. top:((barHeight - bglineSize) / 2)+'rpx',
  27. height:bglineSize+'rpx',
  28. borderRadius:borderRadius}"></view>
  29. <text
  30. class="gui-sg-slider-bar gui-block"
  31. :class="barClass"
  32. :style="{
  33. width:barWidth+'rpx',
  34. height:barHeight+'rpx',
  35. 'line-height':barHeight+'rpx',
  36. left:left+'px',
  37. fontSize:barTextSize,
  38. borderRadius:borderRadius
  39. }">{{barText}}</text>
  40. </view>
  41. </template>
  42. <script>
  43. // #ifdef APP-NVUE
  44. const dom = weex.requireModule('dom');
  45. // #endif
  46. export default{
  47. name : "gui-single-slider",
  48. props : {
  49. barHeight : {type:Number, default:38},
  50. barWidth : {type:Number, default:158},
  51. barClass : {type:Array, default:function(){return ['gui-bg-primary', 'gui-color-white'];}},
  52. bglineSize : {type:Number, default:2},
  53. bglineClass : {type:Array, default:function(){return ['gui-bg-primary'];}},
  54. bglineAClass : {type:Array, default:function(){return ['gui-bg-primary'];}},
  55. barText : {type:String, default:''},
  56. barTextSize : {type:String, default:'20rpx'},
  57. borderRadius : {type:String, default:'32rpx'},
  58. canSlide : {type:Boolean, default:true}
  59. },
  60. data() {
  61. return {
  62. left : 0,
  63. startLeft : 0,
  64. width : 0,
  65. barWidthPX : 30
  66. }
  67. },
  68. mounted:function(){
  69. this.init();
  70. },
  71. methods:{
  72. init : function(){
  73. // #ifdef APP-NVUE
  74. var el = this.$refs.gracesgslider;
  75. dom.getComponentRect(el, (res) => {
  76. if(!res.result || res.size.width < 5){
  77. setTimeout(()=>{this.init();}, 100);
  78. return;
  79. }
  80. this.startLeft = res.size.left;
  81. this.width = res.size.width;
  82. this.barWidthPX = uni.upx2px(this.barWidth);
  83. });
  84. // #endif
  85. // #ifndef APP-NVUE
  86. uni.createSelectorQuery().in(this).select('#gracesgslider').fields(
  87. {size: true, rect:true}, (res) => {
  88. if(res == null){
  89. setTimeout(()=>{this.init();}, 100);
  90. return;
  91. }
  92. this.startLeft = res.left;
  93. this.width = res.width;
  94. this.barWidthPX = uni.upx2px(this.barWidth);
  95. }
  96. ).exec();
  97. // #endif
  98. },
  99. touchstart : function (e) {
  100. if(!this.canSlide){return ;}
  101. var touch = e.touches[0] || e.changedTouches[0];
  102. //this.changeBar(touch.pageX);
  103. },
  104. touchmove : function (e) {
  105. if(!this.canSlide){return ;}
  106. var touch = e.touches[0] || e.changedTouches[0];
  107. this.changeBar(touch.pageX);
  108. },
  109. touchend : function (e) {
  110. if(!this.canSlide){return ;}
  111. var touch = e.touches[0] || e.changedTouches[0];
  112. this.changeBar(touch.pageX, true);
  113. },
  114. changeBar : function(x){
  115. var left = x - this.startLeft;
  116. if(left <= 0){
  117. this.left = 0;
  118. this.$emit('change', 0);
  119. }else if(left + this.barWidthPX > this.width){
  120. left = this.width - this.barWidthPX;
  121. this.left = left;
  122. this.$emit('change', 100);
  123. }else{
  124. this.left = left;
  125. var scale = this.left / (this.width - this.barWidthPX);
  126. this.$emit('change', Math.round(scale * 100));
  127. }
  128. },
  129. setProgress : function (value){
  130. if(this.width < 1){ setTimeout(()=>{this.setProgress(value), 300}); return ;}
  131. if(value < 0){value = 0;}
  132. if(value > 100){value = 100;}
  133. this.left = ( value / 100 ) * (this.width - this.barWidthPX);
  134. }
  135. },
  136. emits:['change']
  137. }
  138. </script>
  139. <style scoped>
  140. .gui-sg-slider{overflow:hidden; position:relative;}
  141. .gui-sg-slider-a-line{position:absolute; left:0; top:0;}
  142. .gui-sg-slider-bar{position:absolute; left:0; top:0; border-radius:50rpx; font-size:20rpx; text-align:center; color:#323232; overflow:hidden;}
  143. </style>