gui-single-slider.vue 3.9 KB

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