gui-interval-slider.vue 4.4 KB

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