graceNvueSlider.vue 4.4 KB

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