gui-interval-slider.vue 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. <template>
  2. <view
  3. class="grace-slider"
  4. :style="{
  5. height:blockSize+'px', width:realWidth+'px'
  6. }"
  7. @touchmove.stop.prevent="touchmove">
  8. <view
  9. class="grace-slider-bar"
  10. :style="{
  11. backgroundColor:bgColor,
  12. width:realWidth+'px',
  13. height:height+'px',
  14. top:lineTop+'px'
  15. }"></view>
  16. <view
  17. class="grace-slider-active-bar"
  18. :style="{
  19. backgroundColor:activeColor,
  20. width:activeWidth+'px',
  21. height:height+'px',
  22. marginLeft:activeLeft+'px',
  23. top:lineTop+'px'
  24. }"></view>
  25. <view
  26. class="grace-slider-block"
  27. :style="{
  28. backgroundColor:blockBgColor,
  29. height:blockSize+'px',
  30. width:blockSize+'px',
  31. 'margin-left':xMin+'px'
  32. }"
  33. @touchstart.stop="touchstart"
  34. data-tag="min"></view>
  35. <view
  36. class="grace-slider-block"
  37. :style="{
  38. backgroundColor:blockBgColor,
  39. height:blockSize+'px',
  40. width:blockSize+'px',
  41. 'margin-left':xMax+'px'
  42. }"
  43. @touchstart.stop="touchstart"
  44. data-tag="max"></view>
  45. </view>
  46. </template>
  47. <script>
  48. const _windowWidth = uni.getSystemInfoSync().windowWidth;
  49. export default {
  50. name : "gui-interval-slider",
  51. data() {
  52. return {
  53. realWidth : 200,
  54. realMax : 200,
  55. xMin : 0,
  56. left1 : 0,
  57. xMax : 100,
  58. currentBlock : '',
  59. minValue : 0,
  60. maxValue : 100,
  61. activeWidth : 0,
  62. activeLeft : 0,
  63. lineTop : 0
  64. };
  65. },
  66. props: {
  67. bgColor : {type : String, default : "#F6F7F8"},
  68. activeColor : {type : String, default : "#3688FF"},
  69. width : {type : Number, default : 750},
  70. height : {type : Number, default : 2},
  71. blockBgColor : {type : String, default : "#FFFFFF"},
  72. blockSize : {type : Number, default : 32},
  73. min : {type : Number, default : 0},
  74. minDefault : {type : Number, default : 0},
  75. max : {type : Number, default : 100},
  76. maxDefault : {type : Number, default : 100}
  77. },
  78. created:function(){
  79. this.realWidth = this.upx2px(this.width);
  80. this.left1 = (_windowWidth - this.realWidth) / 2;
  81. this.realMax = this.realWidth - this.blockSize;
  82. this.lineTop = (this.blockSize - this.height) / 2;
  83. this.setSlider();
  84. },
  85. watch:{
  86. minDefault : function(){
  87. this.setSlider();
  88. },
  89. maxDefault : function(){
  90. this.setSlider();
  91. }
  92. },
  93. methods: {
  94. setSlider : function(){
  95. this.xMin = (this.minDefault - this.min) / (this.max - this.min) * this.realMax;
  96. this.xMax = (this.maxDefault - this.min) / (this.max - this.min) * this.realMax;
  97. this.minValue = this.minDefault;
  98. this.maxValue = this.maxDefault;
  99. this.activeLeft = this.xMin + 5;
  100. this.activeWidth = this.xMax - this.xMin;
  101. },
  102. touchstart: function(e) {
  103. this.currentBlock = e.target.dataset.tag;
  104. if(this.currentBlock == 'min'){
  105. var pageX = e.pageX || e.changedTouches[0].pageX;
  106. pageX = pageX - this.left1;
  107. this.xMin = pageX;
  108. }else{
  109. var pageX = e.pageX || e.changedTouches[0].pageX;
  110. pageX = pageX - this.left1;
  111. this.xMax = pageX;
  112. }
  113. },
  114. touchmove: function(e) {
  115. this.calculate(e);
  116. },
  117. px2upx: function(px) {
  118. return (750 / _windowWidth ) * px;
  119. },
  120. upx2px : function(upx){
  121. return (_windowWidth / 750) * upx;
  122. },
  123. calculate: function(e) {
  124. var pageX = e.pageX || e.changedTouches[0].pageX;
  125. pageX = pageX - this.left1;
  126. if(this.currentBlock == 'min'){
  127. var xMin = this.xMin + (pageX - this.xMin);
  128. // 最左侧限制
  129. if(xMin < 0){xMin = 0;}
  130. // 最右侧限制
  131. if(xMin >= this.realMax){xMin = this.realMax;}
  132. this.xMin = xMin;
  133. // 计算值
  134. var value = this.xMin / this.realMax * (this.max - this.min);
  135. value = parseInt(value);
  136. value = value + this.min;
  137. this.minValue = value;
  138. }else{
  139. var xMax = this.xMax + (pageX - this.xMax);
  140. // 最左侧限制
  141. if(xMax < 0){xMax = 0;}
  142. // 最右侧限制
  143. if(xMax >= this.realMax){xMax = this.realMax;}
  144. this.xMax = xMax;
  145. // 计算值
  146. var value = this.xMax / this.realMax * (this.max - this.min);
  147. value = parseInt(value);
  148. value = value + this.min;
  149. this.maxValue = value;
  150. }
  151. // 获得2个值并传递出去
  152. if(this.maxValue >= this.minValue){
  153. this.$emit('change', [this.minValue, this.maxValue]);
  154. }else{
  155. this.$emit('change', [this.maxValue, this.minValue]);
  156. }
  157. }
  158. }
  159. };
  160. </script>
  161. <style scoped>
  162. .grace-slider{position:relative;}
  163. .grace-slider-bar{position:absolute; top:0rpx;}
  164. .grace-slider-block{position:absolute; top:0rpx; left:0; border-radius:500px; border-width:1px; border-style:solid; border-color:#F5F6F8;}
  165. .grace-slider-active-bar{position:absolute; top:25rpx;}
  166. </style>